Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 18, 2019 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rust-play/caddbc911141f15bacc22f69b8e97196 to your computer and use it in GitHub Desktop.
Save rust-play/caddbc911141f15bacc22f69b8e97196 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::env;
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::Path;
fn read_file(path: &Path) -> io::Result<String> {
let mut file = File::open(path)?;
let mut s = String::new();
file.read_to_string(&mut s).map(|_| s)
}
fn read_args() -> bool {
eprintln!("Reading from argument file");
env::args()
.map(|file| read_file(Path::new(&file)))
.fold(true, |res, read_res| {
match read_res {
Ok(ref contents) => {
println!("{}", contents);
res
},
Err(_) => false,
}
})
}
fn read_input() -> bool {
eprintln!("Reading from input");
true
}
#[allow(dead_code)]
fn main() -> u32 {
let result = if env::args().len() > 1 {
read_args()
} else {
read_input()
};
result as u32
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment