[][src]Type Definition resultit::TryResult

type TryResult<T> = Result<T, Box<dyn Error + Send + Sync>>;

Shorthand for a Result with a boxed error trait. Provided for convenience, not a dependency of any submodule.

Particularly useful for flattening iterators of nested results by flattening/combining/erasing the error types. See the the crate level documentation for an example of how to do this. A simpler example of what this type does follows below:

// Trivial case in which we return just one error type.
fn parse1(num: &str) -> Result<i32, <i32 as std::str::FromStr>::Err> {
	return num.parse();
}

// What if our function can return more than one error type?
// We can return a boxed error trait to erase the type of the error.
// We can use then use the ? (aka try) operator to propagate errors.
// For maximum compatibility with threaded programs,
// the error should also implement send and sync.
fn parse2(num: &str) -> Result<i32, std::boxed::Box<
	dyn std::error::Error
	+ std::marker::Send
	+ std::marker::Sync
>> {
	// do_something_fallible()?;
	let parsed_num = (num.parse())?;
	return Ok(parsed_num);
}
 
// Same as parse2() but using TryResult<i32> as shorthand.
fn parse3(num: &str) -> resultit::TryResult<i32> {
	// do_something_fallible()?;
	let parsed_num = (num.parse())?;
	return Ok(parsed_num);
}