Created
November 13, 2015 12:30
-
-
Save joonty/bb374e546d4dd835bc6d to your computer and use it in GitHub Desktop.
Reading from stdin 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
src/main.rs:8:36: 8:42 error: cannot borrow immutable local variable `buffer` as mutable | |
src/main.rs:8 try!(stdin.read_to_string(&mut buffer)); | |
^~~~~~ | |
<std macros>:1:1: 6:48 note: in expansion of try! | |
src/main.rs:8:5: 8:45 note: expansion site | |
error: aborting due to previous error |
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::env; | |
use std::io; | |
use std::io::Error; | |
use std::io::prelude::*; | |
fn read_from_stdin(buffer: &mut String) -> Result<(), Error> { | |
let mut stdin = io::stdin(); | |
try!(stdin.read_to_string(&mut buffer)); | |
Ok(()) | |
} | |
fn main() { | |
let mut buffer = String::new(); | |
match read_from_stdin(&mut buffer) { | |
Err(why) => println!("{:?}", why), | |
Ok(_) => println!("{}", buffer), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment