3 ms·
That's is completely false, stop spreading misinformation. This code will compile (see https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=
by cjhopman 7y ago
That's is completely false, stop spreading misinformation.
This code will compile (see https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=050a528df9eecdadf6f65c58d59f62ba https://play.rust-lang.org/?version=stable&mode=debug&editio...):
pub fn foo() -> Result<(), i64> {
Err(1)
}
pub fn bar() -> Result<(), ()> {
foo();
Ok(())
}
It will provide a warning, but there's a ton of stuff in c++ that would throw a warning and you wouldn't say that it "will not compile".
A trivial change that still doesn't handle the error would get rid of the warning.
pub fn foo() -> Result<(), i64> {
Err(1)
}
pub fn bar() -> Result<(), ()> {
println!("{:?}", foo());
Ok(())
}
- eddyb 7y agoI wonder if people upthread meant the warning, or e.g. getting the `Foo` in `Result<Foo, BarError>`. (EDIT: nevermind, just looked again and pcwalton was referring to the warning and specifically `Result<(), E>`; oh well) Because the latter is impossible in Rust and probably more relevant to the usual cited issue with Go's pair approach (i.e. using the null/zeroed `Foo` without checking if there was an error). I do agree though that the warning isn't to stop you from not handling the error at all, it's more of a hint that maybe you forgot something. Printing a `Result` may be the legitimate way to handle it in that case, it's largely left to the user to decide what propagates and what doesn't.