Thanks, Scott, I think that's closer.
However, now I'm having trouble with my pointer types. Using the element
inside a spawn means that I need to capture the owned string in the right
way so that the compiler allows me to give it to the other task.
This version:
~~~
let ports = do myvect.iter().map |s| {
let (pport, cchan) = stream();
do spawn {
cchan.send(fun(*s))
}
pport
};
~~~
gives me pointer-type related errors:
- error: cannot move out of dereference of & pointer
- cchan.send(fun(*s))
- error: cannot borrow immutable local variable as mutable
- when I iterate over the Ports later
- error: cannot capture variable of type `&~str`, which does not fulfill
`Send`, in a bounded closure
- cchan.send(fun(*s))
I also tried a version with |&s| and cchan.send(fun(s)), which gave me
different errors:
- error: cannot move out of captured outer variable in a heap closure
- cchan.send(fun(*s))
- error: cannot move out of dereference of & pointer
- on the |&s|
- error: cannot borrow immutable local variable as mutable
- when I iterate over the Ports later
I'm very new to Rust. What do I need to do to let the compiler know that
I'm not going to use anything in the first vec anymore? That I just want
the ~str pointers directly?
(I put the |s| outside the {} because putting it inside seemed to confuse
things -- in that case, rustc expected an identifier instead of the `let`
that comes next, so I assumed that `do v.iter().map {|s| ...}` is a syntax
error.)
Thanks,
Leah
On Sat, Nov 2, 2013 at 3:23 PM, Scott Lawrence <[email protected]> wrote:
> I would think:
>
> let ports = do myvect.iter().map { |e| something(e) }
>
>
> On Sat, 2 Nov 2013, Leah Hanson wrote:
>
> Hi,
>>
>> I have a ~[~str]. I have code that will turn a ~str into a Port<uint>.
>>
>> I want to end up with a [Port<uint>]. (or ~ or @ or whatever. I just want
>> to be able to iterate over the Ports later.)
>>
>> Since I'm not sure what looping construct to use, I tried with a for-each
>> loop.
>>
>> ~~~
>> let ports = for s in myvect.iter() {
>> let (pport, cchan) = stream();
>> do spawn {
>> cchan.send(fun(*s))
>> }
>> pport
>> };
>> ~~~
>>
>> As you might, expect I got an error:
>> error: mismatched types: expected `()` but found `std::comm::Port<uint>`
>> (expected () but found struct std::comm::Port)
>>
>> From this, I take it that for loops must return `()`, rather than an
>>> actual
>>>
>> value. When I searched for a map function in the documentation, I only
>> found a Map type.
>>
>> How would you map one vector to a vector of a different element type?
>>
>> Thanks,
>> Leah
>>
>>
> --
> Scott Lawrence
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev