Re: [rust-dev] How would you map one vector to a vector of a different element type?
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 Portuint. I want to end up with a [Portuint]. (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::Portuint` (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
Re: [rust-dev] How would you map one vector to a vector of a different element type?
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 Portuint. I want to end up with a [Portuint]. (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::Portuint` (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
Re: [rust-dev] How would you map one vector to a vector of a different element type?
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 Portuint. I want to end up with a [Portuint]. (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::Portuint` (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
Re: [rust-dev] How would you map one vector to a vector of a different element type?
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.auwrote: 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 Portuint. I want to end up with a [Portuint]. (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::Portuint` (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
Re: [rust-dev] How would you map one vector to a vector of a different element type?
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.auwrote: 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 Portuint. I want to end up with a [Portuint]. (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::Portuint` (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
Re: [rust-dev] How would you map one vector to a vector of a different element type?
Le 02/11/2013 21:05, Leah Hanson a écrit : 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? ~str is an owned string that can only have one owner. If you move it (give away ownership) you can not use it anymore. The compiler verifies that. This is why move_iter() consumes a vector (takes ownership), while iter() only gives you references ( pointers) to the elements. This allows the memory to be freed when the owner disappears, without garbage collection or reference counting. ~~~ 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 o 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? I’m not sure, but you may need to use spawn_with to move something into a task: do spawn_with(s) |s| { ... } -- Simon Sapin ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev