borrow-checker

Why do I need to collect into a vector when using `flat_map`?

时光怂恿深爱的人放手 提交于 2020-12-08 08:44:46
问题 I'm working on Project Euler 96 to teach myself Rust. I've written this code to read in the file and convert it into a vector of integers (Playground). let file = File::open(&args[1]).expect("Sudoku file not found"); let reader = BufReader::new(file); let x = reader .lines() .map(|x| x.unwrap()) .filter(|x| !x.starts_with("Grid")) .flat_map(|s| s.chars().collect::<Vec<_>>()) // <-- collect here! .map(|x| x.to_digit(10).unwrap()) .collect::<Vec<_>>(); This all works fine but I'm puzzled why I

Closure requires unique access to lambda function

狂风中的少年 提交于 2020-06-28 08:01:46
问题 I'm learning Rust, and have implemented some simple code to experiment with closures - but am running up against an issue with the borrow-checker that I'm not sure how to resolve. When compiling the following function fn twice(x:int, f:|int| -> int) -> int { f(f(x)) } I get the following error closure requires unique access to `f` but it is already borrowed I'm working through the guide and have a moderate understanding of why the borrow-checker doesn't like this - but I'm unsure how to

How to run for loop on elements of a vector and change the vector inside the for loop and outside the for loop in rust?

耗尽温柔 提交于 2020-06-26 06:02:38
问题 I am new to Rust . I need to create a vector before a for loop. Run for loop on it. Change the vector inside the for loop. Then Change the vector after the for loop. I tried the following code and tried to use immutable borrow but both did not work. fn main() { let mut vec1 = vec![4, 5]; vec1.push(6); for i in vec1 { if i % 2 == 0 { vec1.push(7); } } vec1.push(8); println!("vec1={:?}", vec1); } I expect to compile and change the vector inside and after the for loop. But it shows this error

rust pass box reference without move

雨燕双飞 提交于 2020-06-17 13:14:11
问题 Background: I'm writing a RDBMS in rust The db.catalog maintain a hashmap from table_id to table: pub struct Catalog { table_id_table_map: HashMap<i32, Box<dyn Table>>, } And when I add a boxed table to the catalog, move occurred. Then I can't use table instance anymore: // create table let table = create_random_heap_table(....); // add to catalog db.get_catalog().add_table(Box::new(table), "heap table", ""); // access table instance let table_id = table.get_id(); compile error: error[E0382]:

Rust trait field lifetime

♀尐吖头ヾ 提交于 2020-06-01 04:48:26
问题 I think this is something obvious I'm missing, but here goes.. use std::io; pub trait Source<'a, T> { fn push(&self, t: T) -> io::Result<()>; fn link(&mut self, sink: &dyn Sink<'a, T>) -> io::Result<()>; } pub trait Sink<'a, T> { fn push(&self, t: T) -> io::Result<()>; fn link(&mut self, source: &dyn Source<T>) -> io::Result<()>; } pub struct SyncSource<'a, T> { sink: Option<&'a dyn Sink<'a, T>>, } impl<'a, T> SyncSource<'a, T> { pub fn new() -> SyncSource<'a, T> { SyncSource { sink: None, }

Temporary value is freed at the end of this statement [duplicate]

元气小坏坏 提交于 2020-05-25 07:10:30
问题 This question already has answers here : “borrowed value does not live long enough” when using the builder pattern (1 answer) “borrowed value does not live long enough” seems to blame the wrong thing (2 answers) Why is it legal to borrow a temporary? (3 answers) Why does the compiler tell me to consider using a `let` binding" when I already am? (2 answers) Closed last year . I'm trying to scrape a webpage using the Select crate: let document = Document::from_read(response).unwrap(); for node

Temporary value is freed at the end of this statement [duplicate]

ぐ巨炮叔叔 提交于 2020-05-25 07:09:13
问题 This question already has answers here : “borrowed value does not live long enough” when using the builder pattern (1 answer) “borrowed value does not live long enough” seems to blame the wrong thing (2 answers) Why is it legal to borrow a temporary? (3 answers) Why does the compiler tell me to consider using a `let` binding" when I already am? (2 answers) Closed last year . I'm trying to scrape a webpage using the Select crate: let document = Document::from_read(response).unwrap(); for node