我是 Rust 的新手,我现在正在学习 Rust。当我在 Rust 中编写这样的代码时:
#[macro_use]
extern crate rocket;
extern crate diesel;
#[get("/hello")]
fn index() -> &'static str {
"Hello, world!"
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/hello", routes![index])
}
它工作正常,但是当我切换外部导入序列时:
#[macro_use]
extern crate diesel;
extern crate rocket;
#[get("/hello")]
fn index() -> &'static str {
"Hello, world!"
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/hello", routes![index])
}
我切换了火箭
和柴油
位置,火箭
切换到第二位置, cargo 构建
输出如下所示:
~/Documents/GitHub/reddwarf_music on develop! ⌚ 10:10:04
$ cargo build ‹ruby-2.7.2›
Compiling reddwarf_music v0.1.0 (/Users/dolphin/Documents/GitHub/reddwarf_music)
error: cannot find attribute `launch` in this scope
--> src/main.rs:10:3
|
10 | #[launch]
| ^^^^^^
error: cannot find macro `routes` in this scope
--> src/main.rs:13:22
|
13 | .mount("/hello", routes![index])
| ^^^^^^
|
= note: consider importing this macro:
rocket::routes
error: cannot find attribute `get` in this scope
--> src/main.rs:5:3
|
5 | #[get("/hello")]
| ^^^
warning: unused `#[macro_use]` import
--> src/main.rs:1:1
|
1 | #[macro_use]
| ^^^^^^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error[E0601]: `main` function not found in crate `reddwarf_music`
--> src/main.rs:1:1
|
1 | / #[macro_use]
2 | | extern crate diesel;
3 | | extern crate rocket;
4 | |
... |
13 | | .mount("/hello", routes![index])
14 | | }
| |_^ consider adding a `main` function to `src/main.rs`
error[E0121]: the type placeholder `_` is not allowed within types on item signatures
--> src/main.rs:11:16
|
11 | fn rocket() -> _ {
| ^
| |
| not allowed in type signatures
| help: replace with the correct return type: `Rocket<Build>`
error: aborting due to 5 previous errors; 1 warning emitted
Some errors have detailed explanations: E0121, E0601.
For more information about an error, try `rustc --explain E0121`.
error: could not compile `reddwarf_music`
To learn more, run the command again with --verbose.
(base)
为什么会发生这种情况?这个问题让我很困惑。这是我的 Cargo.toml
:
[package]
name = "reddwarf_music"
version = "0.1.0"
edition = "2018"
[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }
rand = "0.8.4"
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
reqwest = "0.11.4"
# database
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0"
最佳答案
诸如#[macro_use]
之类的属性适用于紧随其后的项目。所以这个:
#[macro_use]
extern crate rocket;
extern crate diesel;
表示将 macro_use
属性应用于 rocket
,而:
#[macro_use]
extern crate diesel;
extern crate rocket;
表示将macro_use
属性应用于diesel
。
如果您想将 macro_use
应用于两者:
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate rocket;
关于rust - 是 Rust 中的外部 crate 序列影响吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68915967/