edition = "2018" 使用或不使用 `extern crate <name>`
edition = "2018" use or not use `extern crate <name>`
我想序列化一个结构,将它打印到标准输出,从另一个程序读取它并反序列化。我发现我可以使用 serde crate and Bincode 作为数据格式。
我想到了这个例子:
#[macro_use]
extern crate serde;
use bincode::{deserialize, serialize};
#[derive(Serialize, Deserialize)]
struct Entity {
x: f32,
y: f32,
}
#[derive(Serialize, Deserialize)]
struct World(Vec<Entity>);
fn main() {
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);
let encoded: Vec<u8> = serialize(&world).unwrap();
println!("{:?}", encoded);
let decoded: World = deserialize(&encoded[..]).unwrap();
}
在 Cargo.toml
我有:
[package]
name = "test"
version = "0.1.0"
edition = "2018"
[dependencies]
bincode = "1.1.4"
serde = { version = "1.0", features = ["derive"] }
但让我感到困惑的是,即使我已经声明使用 edition = "2018"
并且根据我的理解,这意味着 extern crate serde;
可以省略,如果我删除以下行:
#[macro_use]
extern crate serde;
我收到多个错误,例如:
error: cannot find derive macro `Deserialize` in this scope
--> src/main.rs:3:21
|
3 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
error: cannot find derive macro `Serialize` in this scope
--> src/main.rs:3:10
|
3 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^
error: cannot find derive macro `Deserialize` in this scope
--> src/main.rs:9:21
|
9 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
error: cannot find derive macro `Serialize` in this scope
--> src/main.rs:9:10
|
9 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^
因此想知道何时或如何使用 edition = "2018"
#[macro_use]
的使用让我想起了 Python 中的装饰器,可能是相同的逻辑适用于 Rust,或者正在进行更多标准化语言的工作,以便可能在 edition = "20XX
不需要 #[macro_use]
?
#[macro_use]
extern crate serde;
我正在使用 Rust 1.35.0。
在 2018 版中,程序宏与其他任何项目一样,因此您必须将它们纳入范围:
use serde::{Serialize, Deserialize};
我想序列化一个结构,将它打印到标准输出,从另一个程序读取它并反序列化。我发现我可以使用 serde crate and Bincode 作为数据格式。
我想到了这个例子:
#[macro_use]
extern crate serde;
use bincode::{deserialize, serialize};
#[derive(Serialize, Deserialize)]
struct Entity {
x: f32,
y: f32,
}
#[derive(Serialize, Deserialize)]
struct World(Vec<Entity>);
fn main() {
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);
let encoded: Vec<u8> = serialize(&world).unwrap();
println!("{:?}", encoded);
let decoded: World = deserialize(&encoded[..]).unwrap();
}
在 Cargo.toml
我有:
[package]
name = "test"
version = "0.1.0"
edition = "2018"
[dependencies]
bincode = "1.1.4"
serde = { version = "1.0", features = ["derive"] }
但让我感到困惑的是,即使我已经声明使用 edition = "2018"
并且根据我的理解,这意味着 extern crate serde;
可以省略,如果我删除以下行:
#[macro_use]
extern crate serde;
我收到多个错误,例如:
error: cannot find derive macro `Deserialize` in this scope
--> src/main.rs:3:21
|
3 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
error: cannot find derive macro `Serialize` in this scope
--> src/main.rs:3:10
|
3 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^
error: cannot find derive macro `Deserialize` in this scope
--> src/main.rs:9:21
|
9 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
error: cannot find derive macro `Serialize` in this scope
--> src/main.rs:9:10
|
9 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^
因此想知道何时或如何使用 edition = "2018"
#[macro_use]
的使用让我想起了 Python 中的装饰器,可能是相同的逻辑适用于 Rust,或者正在进行更多标准化语言的工作,以便可能在 edition = "20XX
不需要 #[macro_use]
?
#[macro_use]
extern crate serde;
我正在使用 Rust 1.35.0。
在 2018 版中,程序宏与其他任何项目一样,因此您必须将它们纳入范围:
use serde::{Serialize, Deserialize};