【问题标题】:Instantiating a 2d Vec in a Struct?在结构中实例化 2d Vec?
【发布时间】:2015-03-24 14:34:01
【问题描述】:

在使用构造函数返回新的结构对象时,我无法实例化 vec。我尝试过的语法(可能不正确地使用 collect())会产生大量编译器错误。

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
}

我收到以下错误:

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);
                                                        ^~

【问题讨论】:

  • 你一直在使用什么语法?
  • 我将用我刚刚尝试过的内容更新帖子
  • 有什么错误?
  • 再次更新帖子出现错误

标签: rust


【解决方案1】:

Vladimir 的回答非常好,但是我感觉功能样式可能会在这里隐藏错误。

您实际上离解决方案不远;问题只是你不能在外循环的每次迭代中重用相同的t2。因此,最简单的转换是在 inside 外部循环中创建 t2

impl Level {
    fn new() -> Level {
        Level {
            tiles: {
            let mut t = Vec::new();
            for x in range(0, 80) {
                let mut t2 = Vec::new(); // Moved!
                for y in range(0, 24) {
                    t2.push(Tile::new(x, y));
                }
                t.push(t2);
            }
            t
        }
    }
}

【讨论】:

  • 嗯,“函数式风格”是现在惯用方法,也就是说,将来大部分代码都会这样写:)
  • @VladimirMatveev:我同意,而且它也更加简洁明了;但是我担心它可能比 OP 当前的代码高出太多,所以我更愿意指出 exactly 除了建议改变风格之外,当前的问题是什么(或者我的意思是,让您推荐改变风格)。
  • 我最终使用了 Vladimir 的解决方案,但您的解决方案帮助我准确理解了我做错了什么 - 谢谢!
【解决方案2】:

是的,你做错了。类似的代码在 C/C++ 中也会不正确,顺便说一句。

        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());
            }
            t.push(t2);
        }

问题是,你总是在内部循环中推入相同的t2,然后你总是将相同的t2 推入t。后者违反了所有权语义,因此 Rust 编译器正确地告诉你如何使用移动值。

惯用的方法是使用迭代器,它可能看起来像这样:

(0..80).map(|_| (0..24).map(|_| Tile::new()).collect()).collect()

如果您需要访问索引,您可以使用map() 闭包参数:

(0..80).map(|x| (0..24).map(|y| Tile::new(x, y)).collect()).collect()

编译器应该自动推导出collect()结果的所需类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 1970-01-01
    相关资源
    最近更新 更多