I'm having trouble instantiating a vec when using a constructor to return a new struct object. The syntax I've tried (using collect() improperly, probably) spat out a ton of compiler errors.
fn main() {
let level = Level::new();
}
struct Level {
tiles: Vec<Vec<Tile>>
}
struct Tile {
idx: i32
}
impl Level {
fn new() -> Level {
Level {
tiles: {
let mut t = Vec::new();
let mut t2 = Vec::new();
for x in range(0, 80) {
for y in range(0, 24) {
t2.push(Tile::new(x, y));
}
t.push(t2);
}
t
}
}
}
impl Tile {
fn new(x: i32, y: i32) -> Tile {
Tile { pos: Point { x: x, y: y } }
}
}
struct Point {
x: i32,
y: i32
}
I get these errors:
src/game/dungeon/level/mod.rs:47:25: 47:27 error: use of moved value: `t2`
src/game/dungeon/level/mod.rs:47 t2.push(Tile::new(x, y));
^~
src/game/dungeon/level/mod.rs:49:28: 49:30 note: `t2` moved here because it has type `collections::vec::Vec<game::dungeon::level::Tile>`, which is non-copyable
src/game/dungeon/level/mod.rs:49 t.push(t2);
^~
src/game/dungeon/level/mod.rs:49:28: 49:30 error: use of moved value: `t2`
src/game/dungeon/level/mod.rs:49 t.push(t2);
^~
src/game/dungeon/level/mod.rs:49:28: 49:30 note: `t2` moved here because it has type `collections::vec::Vec<game::dungeon::level::Tile>`, which is non-copyable
src/game/dungeon/level/mod.rs:49 t.push(t2);
^~
Yes, you're doing it incorrectly. The similar code will also be incorrect in C/C++, BTW.
The problem is, you're always pushing into the same
t2
in the inner loop and then you're always pushing the samet2
intot
. The latter is a violation of ownership semantics, so Rust compiler correctly tells you about using a moved value.The idiomatic approach is to use iterators and it could look like this:
If you need to access indices you can use
map()
closure arguments:The compiler should automatically deduce the desired type of
collect()
result.Vladimir's answer is really nice, however I have a feeling that the functional style might hide the error here.
You are actually not far from the solution; the issue is simply that you cannot reuse the same
t2
at each iteration of the outer loop. The simplest transformation, therefore, is to createt2
inside the outer loop: