Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Rust Result Error Example

This was a quick test to make sure I'm getting the basics of Rust's Result enum type. I tried to do this in an actual app but kept getting errors saying "expected 1 generic argument", "supplied 2 generic arguments".

Code

fn main() {
  match is_ok() {
    Ok(text) => println!("Ok: {}", text),
    Err(e) => println!("Error: {}", e),
  }
  match is_err() {
    Ok(text) => println!("Ok: {}", text),
    Err(e) => println!("Error: {}", e),
  }
}

fn is_ok() -> Result<String, &'static str> {
  Ok("it worked".to_string())
}

fn is_err() -> Result<String, &'static str> {
  Err("it failed")
}

Results

Ok: it worked
Error: it failed