3 ms·
In Rust ? is the try/error propagation operator. It converts the error to the correct type, if necessary, and returns it. let ok = return_result()?; and
by zRedShift 6y ago
In Rust ? is the try/error propagation operator. It converts the error to the correct type, if necessary, and returns it.
let ok = return_result()?;
and
let ok = match return_result() {
Ok(ok) => ok,
Err(err) => return Err(err.into()),
};
are equivalent
In TypeScript/Kotlin/C#/PHP and many more languages, ?./?[]/?-> is the null conditional operator.
Rust doesn't have it in operator form (? works on Option<_> types as if they were a degenerate Result<_, ()> type). instead, you "flatmap" the Options with and_then() (if it's fallible) and map() if it's infallible. Or just use bare pattern matching.
$country = $session?->user?->getAddress()?->country;
is
let country = session
.and_then(|session| session.user)
.and_then(|user| user.get_address())
.map(|address| address.country);