4 ms·
`try!(foo)` is roughly equivalent to this (invalid) code: foo().unwrap_or_else({ |e| return Err(e) }) Or in Go terms: a := try!(foo) is equivalent t
by Dobbs 10y ago
`try!(foo)` is roughly equivalent to this (invalid) code:
foo().unwrap_or_else({ |e| return Err(e) })
Or in Go terms:
a := try!(foo)
is equivalent to:
a, err := foo()
if err != nil {
return nil, err
}
Basically it will take the result of foo, and if foo returns an error it will return the error itself.
https://doc.rust-lang.org/beta/std/result/ https://doc.rust-lang.org/beta/std/result/ has lots of examples of alternative ways you can deal with the error case, or the Ok() case of a result.
- zphds 10y agoIt's worth mentioning that try! is a macro that basically takes that expr and turns it to something similar to your go example.