Hi, I'll preface this by saying I'm new to rust in general as well as rocket, so judge this with a grain of salt (feedback very welcomed). I wanted to do a small learner project so I'm using a simple mongodb connection. I've uploaded the entire repo here:
https://github.com/thedewpoint/expense-api/blob/main/src/main.rs
I was able to get everything working, but whenever I build I receive a warning message
warning: unused std::result::Result that must be used
--> src/main.rs:31:5
|
31 | rocket::ignite().mount("/", routes![index,test]).launch().await;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_must_use)] on by default
= note: this Result may be an Err variant, which should be handled
I could add that attribute (or whatever its called) to disable the linting but i want to keep it on. Am I doing something inherently wrong? I've spent a couple hours just digging through the rocket documentation as well as googling and haven't found any examples that use the async api and explicitly call this out. Any advice would be appreciated.
You're ignoring the return value of that statement which is a Result. In Rust, you can think of this as not handing errors that may come up from that statement. This kicks off a warning.
Take a look at this link for more info:
https://doc.rust-lang.org/std/result/#results-must-be-used
I'm not experienced with Rocket so I'm not sure what a more proper way to handle this Result would be though.
@nelsonjchen yea I figured that much, but there is nothing I can do with this return thing, this is just where the api gets bootstrapped, the return actually isn't used
If all that your main() function does is setting up and then launching a Rocket instance, the #[launch] attribute was added to make this common use case simpler: https://github.com/SergioBenitez/Rocket/blob/8da034ab835ef1d599cd146164dffda960275c06/site/guide/3-overview.md#launching. If for any reason you can't or don't want to use #[launch]:
The warning message appears because the launch() function returns a Result, and Results are always "must use". A very common error condition is a configuration or connection error at startup - which will automatically log error messages to the console. So if you don't have any additional error handling of your own to do, it's reasonable to discard the result: let _ = rocket::ignite()...launch().await;. This is useful because let _ = and let _some_variable = are excluded from the unused_must_use warning.
@jebrosen thanks so much, this is very helpful!
Edit: I used the launch attribute and it worked perfect. thanks again!
fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", routes![index,test])
}
Most helpful comment
If all that your
main()function does is setting up and then launching a Rocket instance, the#[launch]attribute was added to make this common use case simpler: https://github.com/SergioBenitez/Rocket/blob/8da034ab835ef1d599cd146164dffda960275c06/site/guide/3-overview.md#launching. If for any reason you can't or don't want to use#[launch]:The warning message appears because the
launch()function returns aResult, andResults are always "must use". A very common error condition is a configuration or connection error at startup - which will automatically log error messages to the console. So if you don't have any additional error handling of your own to do, it's reasonable to discard the result:let _ = rocket::ignite()...launch().await;. This is useful becauselet _ =andlet _some_variable =are excluded from theunused_must_usewarning.