https://github.com/actix/exam...
#[actix_web::main]
#[cfg(test)]
这两个是什么语法?有什么文档可以提供参考的吗?
https://github.com/actix/exam...
#[actix_web::main]
#[cfg(test)]
这两个是什么语法?有什么文档可以提供参考的吗?
[actix_web::main]
链接:https://docs.rs/actix-web/lat...
Explanation: Marks async function to be executed by Actix system.
Examples
#[actix_web::main]
async fn main() {
async { println!("Hello world"); }.await
}
[cfg(test)]
链接: https://doc.rust-lang.org/boo...
Explanation: The #[cfg(test)] annotation on the tests module tells Rust to compile and run the test code only when you run cargo test, not when you run cargo build.
Examples
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}
这是典型的装饰器, 组合模式, 通过宏, 对你的代码块添加额外的功能, 简化代码.
你可以了解一下 cargo expand
, 可以看到宏展开后的实际代码.
这是Rust的宏语法。
7 回答5.3k 阅读
1 回答3.3k 阅读
2 回答827 阅读
1 回答871 阅读
Attributes
An attribute is metadata applied to some module, crate or item. This metadata can be used to/for:
mark functions that will be part of a benchmark
Attributes can take arguments with different syntaxes:
😊 More detail about this:
rust-by-example
the reference