Instead of `do spawn` try `do spawn_with(s) |s| { ... }`. Then the closure
won't close over s, but rather take it as an argument, allowing it to move
it out.

Cheers


On Sat, Nov 2, 2013 at 5:05 PM, Leah Hanson <astriea...@gmail.com> wrote:

> Thanks, Brendan. :)
>
> You’re trying to move the ~strs out of the vector. You’ll need to use
>> `move_iter`:
>>
> Is this "moving" about moving memory around or about promising the
> compiler that I won't use those elements of the vector again?
>
>
>> ~~~
>> let ports = do myvect.move_iter().map |s| {
>>     let (pport, cchan) = stream();
>>     do spawn {
>>         cchan.send(fun(s))
>>     }
>>     pport
>> }.to_owned_vec();
>>
>
> There is still a problem because something (I guess the `do spawn {...}`)
> is a "heap closure".
>
>    - error: cannot move out of captured outer variable in a heap closure
>    - cchan.send(fun(s))
>
> I think that this error message is complaining because I'm trying to move
> s, the ~str that the `do spawn {...}` closes over, to a new task.
> And I'm not allowed to move it because it is apparently a heap closure.
> Is there some other kind of closure that does work here? Or some way to
> make this not a closure?
>
> Thanks,
> Leah
>
>
>
> On Sat, Nov 2, 2013 at 4:24 PM, Brendan Zabarauskas 
> <bjz...@yahoo.com.au>wrote:
>
>> You’re trying to move the ~strs out of the vector. You’ll need to use
>> `move_iter`:
>>
>> ~~~
>> let ports = do myvect.move_iter().map |s| {
>>     let (pport, cchan) = stream();
>>     do spawn {
>>         cchan.send(fun(s))
>>     }
>>     pport
>> }.to_owned_vec();
>> ~~~
>>
>> Also note the use of `to_owned_vec`. `map` lazily returns a `Map`
>> iterator - you need to explicitly drain it.
>>
>> (Note I haven’t tried compiling this)
>>
>> ~Brendan
>>
>> On 3 Nov 2013, at 7:04 am, Leah Hanson <astriea...@gmail.com> wrote:
>>
>> > 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 <byt...@gmail.com>
>> 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
>> > Rust-dev@mozilla.org
>> > https://mail.mozilla.org/listinfo/rust-dev
>>
>>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to