4 ms·
> But what's not obvious is what happens if it did not work? What happens then? Where does the error object go? Is there something that represents the error? Hi
by kitkat_new 4y ago
> But what's not obvious is what happens if it did not work? What happens then? Where does the error object go? Is there something that represents the error? Hidden magic isn't good magic, in my opinion. Just be explicit.
the question mark operator simply checks if there was an error, and if there was one, it returns it. Practically, it does the same as the GO Code - and the only reason you don't know is, because you don't know Rust.
> I don't know Rust, but the example you've given implies I can not include "?" in the call and just ignore any errors entirely?
no, omitting the ? will give you a compiler error. You can ignore the error case by writing:
res1 = canFail().expect("xz didn't work");
or
res1 = canFail().unwrap();
If there's an error, the program will crash. There are other alternatives, which e.g. allow you to set 'res1' to a default value in the error case.
> Go, in comparison, does the opposite: it forces you to handle the error or literally ignore it. You cannot forget to deal with an error in Go.
As you see, there is no difference to Go... in this regard.