rust - 是 Rust 中的外部 crate 序列影响吗

标签 rust

我是 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/

相关文章:

rust - 如何编写显示错误并退出程序的函数的签名?

multithreading - rust tokio::spawn 在 mutexguard 之后等待

rust - 在循环中维护一个 TcpStreams 的 HashMap

rust - 一次借用切片的索引和结构的字段时无法推断正确的生存期

rust - 什么是非词汇生命周期?

mysql - 无法使用柴油箱从 mysql 数据库加载结果

rust - 在函数体上迭代具有生命周期的通用值时,借用值的生命周期不够长

rust - 尝试将盒装 dyn 特性传递给函数时出现 "borrowed value does not live long enough"错误

rust - 为什么 for 循环不要求迭代器是可变的?

struct - 是否有可能有一个结构包含对生命周期比结构短的值的引用?