-
-
Save donabrams/2d49d30f401498eca164 to your computer and use it in GitHub Desktop.
Shared via 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::io; | |
use std::io::prelude::*; | |
use std::io::BufReader; | |
use std::fs::File; | |
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 main(){ | |
match get_line_vec("yay.txt") { | |
Ok(vec) => { | |
let a = vec.pop().unwrap(); | |
println!("{}", a); | |
}, | |
Err(e) => { | |
println!("Error: {}", e.to_string()) | |
} | |
} | |
} |
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::io; | |
use std::io::prelude::*; | |
use std::io::BufReader; | |
use std::fs::File; | |
fn main(){ | |
match get_line_vec("yay.txt") { | |
Ok(vec) => { | |
let mut mut_vec = vec; | |
let a = mut_vec.pop().unwrap(); | |
println!("{}", a); | |
}, | |
Err(e) => { | |
println!("Error: {}", e.to_string()) | |
} | |
} | |
} | |
fn get_line_vec(filename: &str) -> Result<Vec<String>, io::Error> { | |
let f = try!(File::open(filename)); | |
let vec: Vec<String> = BufReader::new(f).lines() | |
.map(|line| line.unwrap()) | |
.collect(); | |
Ok(vec) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
:11:10: 11:13 error:
vec
does not live long enough:11 Ok(&mut vec)
^~~
:6:72: 12:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 6:71...
: 6 fn get_line_vec(filename: &str) -> Result<&mut Vec, io::Error> {
: 7 let mut vec: Vec = Vec::new();
: 8 let mut f = try!(File::open(filename));
: 9 BufReader::new(f).lines()
:10 .map(|line| vec.push(line.unwrap()));
:11 Ok(&mut vec)
...
:7:40: 12:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 7:39
: 7 let mut vec: Vec = Vec::new();
: 8 let mut f = try!(File::open(filename));
: 9 BufReader::new(f).lines()
:10 .map(|line| vec.push(line.unwrap()));
:11 Ok(&mut vec)
:12 }
error: aborting due to previous error
playpen: application terminated with error code 101