Skip to content

Instantly share code, notes, and snippets.

@jmsdnns
Last active November 18, 2024 01:53
Show Gist options
  • Save jmsdnns/7dbea48485f36f4c656212e962062675 to your computer and use it in GitHub Desktop.
Save jmsdnns/7dbea48485f36f4c656212e962062675 to your computer and use it in GitHub Desktop.
Trying to help my friends in the midwest feel comfortable with Rust
use std::fs::File;
enum ComeWith<T, E> {
YouBetcha(T),
Ope(E),
}
impl<T, E> From<std::result::Result<T, E>> for ComeWith<T, E> {
fn from(r: std::result::Result<T, E>) -> Self {
match r {
Ok(v) => ComeWith::YouBetcha(v),
Err(e) => ComeWith::Ope(e),
}
}
}
fn main() {
//let f = ComeWith::from(File::open("doesnt_exist.txt"));
let f = ComeWith::from(File::open("meow.txt"));
let fh = match f {
ComeWith::YouBetcha(file) => file,
ComeWith::Ope(error) => panic!("Can't open file: {:?}", error),
};
println!("File handle: {:?}", fh);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment