try_[method]: Fallible methods with Specific Errors
Prefix for fallible methods that return a Result.
#![allow(unused)] fn main() { impl TryFrom<i32> for u32 { type Error = TryFromIntError; fn try_from(value: i32) -> Result<i64, TryFromIntError>; } impl<T> Receiver<T> { try_recv(&self) -> Result<T, TryRecvError>; } }
- Prefix for methods that can fail, returning a `Result`.
-
TryFromis aFrom-like trait for types whose single-value constructors might fail in some way. -
Ask: Why aren’t
Vec::getand other similar methods calledtry_get?Methods are named
getif they return a reference to an existing value and return anOptioninstead ofResultbecause there is only one failure mode. For example, only “index out of bounds” forVec::get, and “key does not exist” forHashMap::get.