Skip to content

Instantly share code, notes, and snippets.

@joonty
Created November 13, 2015 12:30
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 joonty/bb374e546d4dd835bc6d to your computer and use it in GitHub Desktop.
Save joonty/bb374e546d4dd835bc6d to your computer and use it in GitHub Desktop.
Reading from stdin in rust
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
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