Created
June 18, 2019 18:49
-
-
Save rust-play/caddbc911141f15bacc22f69b8e97196 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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::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