Ultimate Rust Crash Course ^hot^ -
'a means “the returned reference lives at least as long as both inputs.”
For quick prototyping: unwrap() or expect() (panics on error).
A trait defines shared behavior.
use std::fs::File; use std::io::ErrorKind; fn main() let greeting_file_result = File::open("hello.txt"); let greeting_file = match greeting_file_result Ok(file) => file, Err(error) => match error.kind() ErrorKind::NotFound => match File::create("hello.txt") Ok(fc) => fc, Err(e) => panic!("Problem creating the file: :?", e), , other_error => panic!("Problem opening the file: :?", other_error);
enum Result<T, E> Ok(T), Err(E),
When a function returns a reference, the lifetime of the output must be tied to one of the inputs.
let f = File::open("hello.txt").unwrap(); let f = File::open("hello.txt").expect("Failed to open hello.txt"); Propagate errors with ? operator (inside function returning Result ): ultimate rust crash course
pub trait Summary fn summarize(&self) -> String;