Rust tips is a user on octodon.social. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.
Rust tips @rust

For using the `?` operator, the easiest (laziest) function return type is `Result<…, Box<Error>>`.

Almost all errors can be converted to `std::error::Error`, so you can handle many types of errors without having to worry about converting them.

fn foo() -> Result<(), Box<Error>> {
File::create("file")?.write_all(b"hello")?;

if true {
Err("even strings are convertible to boxed errors")?;
}

Ok(())
}

Examples: is.gd/8yeq0z

· Web · 0 · 1