fn save() -> Result<(), String> {
Err("disk full".to_string())
}
fn main() {
save();
println!("done");
}What happens when this is built and run with
cargo run?- aCompilation stops: a
Resultreturned bysave()has to be matched or propagated with? - bIt compiles, since an unused
Resultis only a warning, then printsdone✓ - cIt compiles cleanly and panics at runtime with the message
disk full - dIt compiles cleanly and prints
done; ignoring aResultis silent in Rust
Explanation:
Result is marked #[must_use], so dropping one without inspecting it produces the warning unused Result that must be used. A warning is not an error: the binary builds, save() runs, its Err value is discarded, and done is printed. Nothing panics, because an Err value only panics if you call unwrap/expect on it.