4 ms·
The issue arises if you don't use the returned value. Lets say there's a function "async fn saveToDisk()". You call this function before you exit the program. N
by eis 3y ago
The issue arises if you don't use the returned value. Lets say there's a function "async fn saveToDisk()". You call this function before you exit the program. Now if you forget to use await on it, your program will exit without having saved the data to disk.
- phicoh 3y agoIn any sensible API, saveToDisk would return an error status (a Result type in Rust). If you don't check for errors, then probably you didn't care of the data was actually saved or not.
- veber-alex 3y agoFutures in Rust are annotated with the #[must_use] attribute [1], same as the Result type [2]. This means the compiler will emit a warning (can be upgraded to an error) if you forget to await a future even if it doesn't return anything. [1]: https://doc.rust-lang.org/nightly/src/core/future/future.rs.html#29 https://doc.rust-lang.org/nightly/src/core/future/future.rs.... [2]: https://doc.rust-lang.org/nightly/src/core/result.rs.html#499 https://doc.rust-lang.org/nightly/src/core/result.rs.html#49...
- phicoh 3y agoYou don't want the safety of your program to depend on whether the compiler emits a warning or not. And turning warnings into errors just encourages people to write 'let _ = ...' to get rid of the error.
- veber-alex 3y agoThis has nothing to do with safety, just correctness. > And turning warnings into errors just encourages people to write 'let _ = ...' to get rid of the error. No? writing `let _ = make_future()` will clearly not await the future, why would you do it instead of just adding `.await` ? Using `let _ = ...` is sometimes fine for Result if you really sure you don't care about the potential error you got but it's a no go with futures.
- estebank 3y agoNote that the lint for un-awaited Future doesn't mention the way to silence them by assigning to _: warning: unused implementer of `Future` that must be used --> src/main.rs:9:5 | 9 | foo(); | ^^^^^ | = note: futures do nothing unless you `.await` or poll them = note: `#[warn(unused_must_use)]` on by default