Created
February 14, 2018 15:49
-
-
Save evaporei/3f9f45eef614218ef0e9e673d63a67b5 to your computer and use it in GitHub Desktop.
Cat wannabe in Rust
This file contains hidden or 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; | |
use std::env;// to get command line arguments | |
use std::io::prelude::*; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
if let Some(file_name) = args.get(1) { | |
match File::open(file_name) { | |
Ok(mut file) => { | |
let mut file_content = String::new(); | |
match file.read_to_string(&mut file_content) { | |
Ok(_) => print!("{}", file_content), | |
Err(err) => println!("{}", err), | |
}; | |
}, | |
Err(err) => println!("{}", err), | |
} | |
} else { | |
println!("Should pass file name as second argument"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
melhoras:
aprendizado:
if let
como nomatch
, porém como podemos ver o código ficou um hadouken. Faz mais sentido separar em funções que não tratam o erro e o sobem e no fim as chama utilizando error handling adequado.