Skip to content

Instantly share code, notes, and snippets.

@donabrams
Forked from anonymous/playground.rs
Created December 22, 2015 16:55
Show Gist options
  • Save donabrams/3102f0ab98c4b5a317d6 to your computer and use it in GitHub Desktop.
Save donabrams/3102f0ab98c4b5a317d6 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::collections::HashMap;
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
use std::error::Error;
use std::mem;
use std::str;
type Something = HashMap<String, String>;
fn get_line_vec(filename: &str) -> Result<&mut Vec<String>, io::Error> {
let mut vec: Vec<String> = Vec::new();
let mut f = try!(File::open(filename));
BufReader::new(f).lines()
.map(|line| vec.push(line.unwrap()));
Ok(&mut vec)
}
fn get_something() -> Result<Something, io::Error> {
let mut something = Something::new();
let mut lines = try!(get_line_vec("gates.txt"));
while let Some(line) = lines.pop() {
something.insert(line, line);
}
Ok(something)
}
fn main() {
match get_something() {
Ok(something) => println!("Values: (yay, {})", something.get("yay").unwrap()),
Err(e) => println!("Error: {}", e.to_string()),
}
}
@donabrams
Copy link
Author

:17:10: 17:13 error: vec does not live long enough
:17 Ok(&mut vec)
^~~
:12:72: 18:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 12:71...
:12 fn get_line_vec(filename: &str) -> Result<&mut Vec, io::Error> {
:13 let mut vec: Vec = Vec::new();
:14 let mut f = try!(File::open(filename));
:15 BufReader::new(f).lines()
:16 .map(|line| vec.push(line.unwrap()));
:17 Ok(&mut vec)
...
:13:40: 18:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 13:39
:13 let mut vec: Vec = Vec::new();
:14 let mut f = try!(File::open(filename));
:15 BufReader::new(f).lines()
:16 .map(|line| vec.push(line.unwrap()));
:17 Ok(&mut vec)
:18 }
:24:29: 24:33 error: use of moved value: line [E0382]
:24 something.insert(line, line);
^~~~
:24:29: 24:33 help: see the detailed explanation for E0382
:24:23: 24:27 note: line moved here because it has type collections::string::String, which is non-copyable
:24 something.insert(line, line);
^~~~
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
Compilation failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment