小编Jer*_*iel的帖子

如何在 Vec 中存储引用并稍后在 Rust 中使用它?

我需要创建一个 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(&current);

   current.as_mut().unwrap().x = 2;

   println!("m: {:?}", current);
   println!("stack.m: {:?}", stack.pop());
   current = None;
}
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
3527
查看次数

如何在 Rust 中创建等效的 C 双指针?

我开始将 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)

pointers rust

1
推荐指数
1
解决办法
2350
查看次数

标签 统计

rust ×2

pointers ×1