What do you mean by trust #[xxx] or #![xxx]?

Asked 1 years ago, Updated 1 years ago, 85 views

What does the trust mean by #[xxx] or #![xxx] as stated in the title?

For example,
https://rocket.rs/
It's like the one in this sample code.

#![feature(proc_macro_hygiene,decl_macro)]

# [macro_use] external crate rocket;

# [get("/hello/<name>/<age>")]
fn hello(name:String,age:u8) - > String {
    format!("Hello,{}year old named{}!",age,name)
}

US>fnmain(){
    rocket::ignite().mount("/", routes![hello]).launch();
}

I would appreciate it if you could let me know if there are any names attached to them.
Also, I usually use Ruby and JavaScript, so please let me know if there are any similar features.

I look forward to your kind cooperation.

rust

2022-09-30 19:21

1 Answers

Something like #![hoge] is called Inner attribute, which gives attributes to that scope.
For example, #![feature(proc_macro_hygiene,decl_macro)] in the code indicated means "Enable the unstable features proc_macro_hygiene and decl_macro at this scope."

Something like #[fuga] is called Outer attribute, which gives attributes to the item immediately after it.
For example, #[get("/hello/<name>/>age>")] in the indicated code means "Register the item immediately following (in this case, the function hello) as a handler for the specified URL."
(This get attribute is a language function called attribute-like macro that Rocket uniquely defines.)

I just found out that the symbols and operators that appear frequently in Rust are summarized in The book's appendix.For your information.
https://doc.rust-lang.org/book/appendix-02-operators.html


2022-09-30 19:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.