我需要创建一个 Vec 来跟踪我正在创建的对象并更改其状态以供以后使用。但是,如果我在获取存储在 vec 上的对象时使用克隆,它的状态不会更新,我该怎么做?
#[derive(Debug, Clone)]
struct Movement {
x: i32,
y: i32,
}
fn main() {
let mut current = Some(Movement { x: 1, y: 2 });
let mut stack = Vec::new();
stack.push(¤t);
current.as_mut().unwrap().x = 2;
println!("m: {:?}", current);
println!("stack.m: {:?}", stack.pop());
current = None;
}
Run Code Online (Sandbox Code Playgroud) 我开始将 C 代码移植到 Rust,但我对 Rust 中的工作方式感到困惑。这段代码的等价物是什么:
typedef struct Room {
int xPos;
int yPos;
} Room;
void main (){
Room **rooms;
rooms = malloc(sizeof(Room)*8);
}
Run Code Online (Sandbox Code Playgroud)