我开始学习 Rust 并尝试项目的结构。 现在我有这样的项目结构:
project
└─── src
│ └─── core
│ │ └─── lib.rs
│ │ └─── Cargo.toml
│ └─── api
│ └─── main.rs
│ └─── Cargo.toml
└─── Cargo.toml
toml文件内容如下:
project/Cargo.toml
[workspace]
members = [
"src/core",
"src/api"
]
project/src/api/Cargo.toml
[package]
name = "api"
version = "0.1.0"
[[bin]]
name = "api"
path = "main.rs"
[dependencies]
core = { path = "../core" }
rocket = { version = "0.5.0", features = ["json"] }
使用这些设置,如果在
main.rs
中我正在尝试使用火箭宏,则不会编译该项目。
main.rs
(代码取自火箭包的描述):
#[macro_use] extern crate rocket;
#[get("/<name>/<age>")]
fn hello(name: &str, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/hello", routes![hello])
}
我收到错误:
macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
这是什么原因以及如何解决?
我尝试用src文件夹创建一个api项目,这个问题不再重复。
api
└─── src
│ └─── main.rs
└─── Cargo.toml
但我不想保留每个项目(main 和 lib)的 src 文件夹,因为工作区已经有 src。对此可以做什么?
执行
cargo check
命令的结果:
error: macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
--> src\api\main.rs:4:4
|
4 | fn hello(name: &str, age: u8) -> String {
| ^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #52234 <https://github.com/rust-lang/rust/issues/52234>
note: the macro is defined here
--> src\api\main.rs:3:1
|
3 | #[get("/<name>/<age>")]
| ^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[deny(macro_expanded_macro_exports_accessed_by_absolute_paths)]` on by default
= note: this error originates in the attribute macro `get` (in Nightly builds, run with -Z macro-backtrace for more info)
error: could not compile `api` (bin "api") due to 1 previous error
看起来 Rocket 文档有一段时间没有更新了,因为
#[macro_use]
语法在 Rust 1.30 中被淘汰了。只需通过 use 声明导入这些宏,如下所示:
use rocket::{get, launch, routes};
#[get("/<name>/<age>")]
fn hello(name: &str, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/hello", routes![hello])
}
此代码可以编译。