我想在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) 我来自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,我有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吗?