borrow-checker

Trying to return reference from RwLock, “borrowed value does not live long enough” Error

我是研究僧i 提交于 2019-12-01 07:13:29
问题 I've been working on my first Rust project recently but have hit a snag. I am using a HashMap mapping String s to AtomicUsize integers. The HashMap is protected by a RwLock to allow for concurrent access. I would like to be able to return references to AtomicUsize values in the HashMap , however if I try to return these references to the caller past the lifetime of the RwLockWriteGuard I get an error that borrowed value does not live long enough . I've reproduced a minimal example below and

Can't borrow mutably within two different closures in the same scope

狂风中的少年 提交于 2019-12-01 05:07:06
问题 My goal is to make a function (specifically, floodfill) that works independent of the underlying data structure. I tried to do this by passing in two closures: one for querying, that borrows some data immutably, and another for mutating, that borrows the same data mutably. Example (tested on the Rust Playground): #![feature(nll)] fn foo<F, G>(n: i32, closure: &F, mut_closure: &mut G) where F: Fn(i32) -> bool, G: FnMut(i32) -> (), { if closure(n) { mut_closure(n); } } fn main() { let mut data

Best way to remove elements of Vec depending on other elements of the same Vec

a 夏天 提交于 2019-12-01 04:44:17
问题 I have a vector of sets and I want to remove all sets that are subsets of other sets in the vector. Example: a = {0, 3, 5} b = {0, 5} c = {0, 2, 3} In this case I would like to remove b , because it's a subset of a . I'm fine with using a "dumb" n² algorithm. Sadly, it's pretty tricky to get it working with the borrow checker. The best I've come up with is (Playground): let mut v: Vec<HashSet<u8>> = vec![]; let mut to_delete = Vec::new(); for (i, set_a) in v.iter().enumerate().rev() { for set

Get an enum field from a struct: cannot move out of borrowed content

谁说胖子不能爱 提交于 2019-12-01 03:30:28
I'm new to Rust and trying to wrap my head around the ownership/borrowing concept. Now I have reduced my code to this minimal code sample that gives a compile error. pub struct Display { color: Color, } pub enum Color { Blue = 0x1, Red = 0x4, } impl Display { fn get_color_value(&self) -> u16 { self.color as u16 } } src/display.rs:12:9: 12:13 error: cannot move out of borrowed content src/display.rs:12 self.color as u16 ^~~~ error: aborting due to previous error Could not compile. I'm still in the everything is copied by value mindset, where it is perfectly legal to do self.color as that would

cannot move out of borrowed content when unwrapping a member variable in a &mut self method

依然范特西╮ 提交于 2019-12-01 03:09:05
问题 I was trying to make a Disjoint-Set data structure in Rust. The relevant code is: pub struct Set<'a, T: 'a> { rank: u32, value: T, parent: Option<&'a mut Set<'a, T>>, } impl<'a, T> Set<'a, T> { pub fn find(&'a mut self) -> &'a mut Set<'a, T> { match self.parent { None => self, Some(mut p) => { self.parent = Some(p.find()); self.parent.unwrap() } } } } The errors I get are: error[E0507]: cannot move out of borrowed content --> src/main.rs:9:15 | 9 | match self.parent { | ^^^^ cannot move out

Why nested iterator closures won't copy values from outer scope

你。 提交于 2019-12-01 01:17:01
问题 I'm trying to use nested iterators, where the inner iterator uses value from the outer iterator. vec![0;10].iter().flat_map(|&a| { (0..10).map(|b|{ a + b }) }); error: a does not live long enough (0..10).map(|b|{ ^^^ note: reference must be valid for the method call... This compiles if I move the inner closure ( move |b|{ ), but I don't understand why it is necessary, given that a is an integer and could have been copied instead of moved. 回答1: Both flat_map and map are lazy. The inner map

Is this error due to the compiler's special knowledge about RefCell?

北战南征 提交于 2019-11-30 08:26:44
问题 fn works<'a>(foo: &Option<&'a mut String>, s: &'a mut String) {} fn error<'a>(foo: &RefCell<Option<&'a mut String>>, s: &'a mut String) {} let mut s = "hi".to_string(); let foo = None; works(&foo, &mut s); // with this, it errors // let bar = RefCell::new(None); // error(&bar, &mut s); s.len(); If I put in the two lines with the comment, the following error occurs: error[E0502]: cannot borrow `s` as immutable because it is also borrowed as mutable --> <anon>:16:5 | 14 | error(&bar, &mut s); |

Is there a way to release a binding before it goes out of scope?

不羁岁月 提交于 2019-11-30 02:54:24
问题 I'm trying to parse a file using regexes: extern crate regex; // 1.0.1 use regex::Regex; fn example( section_header_pattern: Regex, section_name: &str, mut line: String, mut is_in_right_section: bool, ) { loop { if let Some(m) = section_header_pattern .captures(&line) .and_then(|c| c.get(1)) { is_in_right_section = m.as_str().eq(section_name); line.clear(); continue; } } } fn main() {} ...but the compiler complains because the RegEx 's captures() method has a borrow which endures for the

Who borrowed a variable?

早过忘川 提交于 2019-11-29 17:36:57
问题 I'm fighting with the borrow checker. I have two similar pieces of code, one working as I expect, and the other not. The one that works as I expect: mod case1 { struct Foo {} struct Bar1 { x: Foo, } impl Bar1 { fn f<'a>(&'a mut self) -> &'a Foo { &self.x } } // only for example fn f1() { let mut bar = Bar1 { x: Foo {} }; let y = bar.f(); // (1) 'bar' is borrowed by 'y' let z = bar.f(); // error (as expected) : cannot borrow `bar` as mutable more // than once at a time [E0499] } fn f2() { let

How do I pass modified string parameters?

社会主义新天地 提交于 2019-11-29 15:38:25
I'm on chapter 12 of The Rust Programming Language , where a case insensitive line search is implemented. It doesn't make sense to me to implement the same logic twice, so I figured if I just called the case sensitive search function with the parameters converted to lower case, that might work. It did not. This is my non working code: fn main() { let a = search("Waldo", "where in\nthe world\nis Waldo?"); let b = search("waldo", "where in\nthe world\nis Waldo?"); let c = search_case_insensitive("waldo", "where in\nthe world\nis Waldo?"); println!("{:?}", a); println!("{:?}", b); println!("{:?}"