Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.