小编teh*_*yit的帖子

如何实现Rust的Copy特性?

我想在Rust中初始化一个结构数组:

enum Direction {
    North,
    East,
    South,
    West,
}

struct RoadPoint {
    direction: Direction,
    index: i32,
}

// Initialise the array, but failed.
let data = [RoadPoint { direction: Direction::East, index: 1 }; 4]; 
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时,编译器抱怨该Copy特征未实现:

error[E0277]: the trait bound `main::RoadPoint: std::marker::Copy` is not satisfied
  --> src/main.rs:15:16
   |
15 |     let data = [RoadPoint { direction: Direction::East, index: 1 }; 4]; 
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `main::RoadPoint`
   |
   = note: the `Copy` trait is required …
Run Code Online (Sandbox Code Playgroud)

traits rust

21
推荐指数
2
解决办法
1万
查看次数

在结构中使用枚举会导致"未解析的名称"错误

我来自C编程背景,并开始学习Rust.

是否可以enum在下面的代码片段中使用结构?

enum Direction {
    EastDirection,
    WestDirection
}

struct TrafficLight {
    direction: Direction,  // the direction of the traffic light
    time_elapse : i32,  // the counter used for the elpase time
}

let mut tl = TrafficLight {direction:EastDirection, time_elapse:0};
Run Code Online (Sandbox Code Playgroud)

当我编译代码时,它抱怨EastDirection不知道.

rust

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

在Rust中指定数组大小时,C的#define相当于什么?

我开始学习Rust,我有C.的背景知识.

我试图将以下代码片段从C转换为Rust.该代码定义了uint8_t一个在编译时定义的大小的数组.

#define GRID_SIZE  100

uint8_t grid[GRID_SIZE];
memset (grid, 0x00, GRID_SIZE);
Run Code Online (Sandbox Code Playgroud)

我在Rust中尝试了以下内容

let grid_size: i32 = 10;
let mut grid: [i32; grid_size] = [0; grid_size];
Run Code Online (Sandbox Code Playgroud)

编译器抱怨说 error: use of undeclared type name `grid_size` [E0412]

查找错误E0412并没有提供任何线索.这可能在Rust吗?

rust

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

标签 统计

rust ×3

traits ×1