Last active
November 18, 2024 01:53
-
-
Save jmsdnns/7dbea48485f36f4c656212e962062675 to your computer and use it in GitHub Desktop.
Trying to help my friends in the midwest feel comfortable with Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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