Skip to content

Instantly share code, notes, and snippets.

@evaporei
Created February 14, 2018 15:49
Show Gist options
  • Save evaporei/3f9f45eef614218ef0e9e673d63a67b5 to your computer and use it in GitHub Desktop.
Save evaporei/3f9f45eef614218ef0e9e673d63a67b5 to your computer and use it in GitHub Desktop.
Cat wannabe in Rust
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");
}
}
@evaporei
Copy link
Author

melhoras:

  • faltam testes
  • falta extração do código, tudo centralizado no main formando um hadouken

aprendizado:

  • não é possível pegar o erro utilizando if let como no match, 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment