lifetime

Cyclic reference of RefCell borrows in traversal

余生颓废 提交于 2019-12-13 16:07:30
问题 I'm learning Rust and tried coding a doubly-linked list. However, I'm stuck already at a typical iterative traversal implementation. I'm getting the impression that the borrow checker / drop checker is too strict and cannot infer the correct lifetime for the borrow when it crosses the function boundary from RefCell . I need to repeatedly set a variable binding ( curr in this case) to the borrow of its current contents: use std::cell::RefCell; use std::rc::Rc; pub struct LinkedList<T> { head:

Dropping partially moved values after the moved values are dropped

蹲街弑〆低调 提交于 2019-12-13 11:10:33
问题 I expect the following code to compile and print Foo(6) as the value of b , owning the reference to a is dropped after the match block. It seems related to this compiler error: error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable --> src/main.rs:26:22 | 13 | let b = get_foo(&mut a); | - mutable borrow occurs here ... 26 | println!("{:?}", a); | ^ immutable borrow occurs here 27 | } | - mutable borrow ends here Dropping the value of b doesn't work either, because

Why do I need to provide lifetimes for a struct's generic parameters which are not members of the struct?

我怕爱的太早我们不能终老 提交于 2019-12-13 03:31:49
问题 I'm trying to get my head around the way that generics and lifetimes interact. Consider: use std::ops::Add; struct Gloop<'a, T: Add> { wumpus: &'a Wumpus<T>, } trait Wumpus<T: Add> { fn fleeb(&self, x: &T) -> bool; } struct Mimsy { jubjub: f64, } impl<T: Add> Wumpus<T> for Mimsy { fn fleeb(&self, x: &T) -> bool { return (x + x) > 0; } } fn main() { let a = Mimsy { jubjub: 1. }; let b = Gloop::<i32> { wumpus: &a }; println!("{}", b.fleeb(1)); } Which yields: error[E0309]: the parameter type `T

C++: constant reference to temporary

落爺英雄遲暮 提交于 2019-12-12 21:42:38
问题 There are several questions about lifetime of constant reference on SO, but still I don't get it. Is this piece of code valid? struct S { const int &ref; S( const int &x ) : ref(x) { } }; int main( ) { S s( 0 ); // ... use( s.ref ); // ... return 0; } Intuitively I'd say no, since 0 should expire after the expression ( S s(0); ) is evaluated. However both GCC and CLANG compile it fine, without warnings, and valgrind doesn't detect any runtime error. What am I missing about references? 回答1:

How to convert an Iterator on a tuple of (String, String) to an Iterator of (&str, &str)?

最后都变了- 提交于 2019-12-12 15:14:40
问题 I'm having trouble converting from an Iterator of (String, String) to an Iterator of (&str, &str) . I'm using an external library, so can't change the signature of that, and not sure that I need to. Basically I have this function def: use hyper; fn build_url<'a, I>(host: &'a str, port: u16, path: &'a str, params: I) -> hyper::Url where I: Iterator<Item=(String, String)> { let mut url = hyper::Url::parse(&format!("http://{h}:{p}/{pt}", h = self.etcd_host, p = self.etcd_port, pt = path)); if

Result of Option::map does not live long enough

我只是一个虾纸丫 提交于 2019-12-12 12:45:02
问题 I expected the two functions below to be equivalent. However the first one does not compile. pub fn does_not_work<I: IntoIterator>(values: I) where I::Item: AsRef<str>, { if let Some(value) = values.into_iter().nth(0).map(|item| item.as_ref()) { if value == "first" { println!("This should print"); } } } pub fn does_work<I: IntoIterator>(values: I) where I::Item: AsRef<str>, { if let Some(value) = values.into_iter().nth(0) { if value.as_ref() == "first" { println!("This should print"); } } }

Can I select a trait object at runtime without using a Box<Trait>?

╄→尐↘猪︶ㄣ 提交于 2019-12-12 12:16:07
问题 I would like to branch and decide on a Trait implementation to use within a function at runtime (see poly_read in the code sample below). The trait object is constructed inside of the branch arms of an if expression and only needs to live for the life of poly_read yet I need to Box it because the trait can't be borrowed from within the expression arm, up to the binding that I'm attempting to assign it to. I understand logically why the borrow ends too early, but it seems like the borrow

Non-cancelable dialog being dismissed on search button click

瘦欲@ 提交于 2019-12-12 10:01:29
问题 I'm showing a non-cancelable dialog in my application, but it gets cancelled if the user presses SEARCH button. I've tried to override onSearchRequested and onKeyDown, but it doesn't help. Any suggestion? 回答1: I also came across this problem and Jamasan's solution did not work for me. I instead added the following code to my custom dialog class (extending Dialog): @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_SEARCH) { return true; } else {

How to specify a lifetime for an Option<closure>?

廉价感情. 提交于 2019-12-12 08:44:14
问题 I'm trying to put a field on a struct that should hold an Option<closure> . However, Rust is yelling at me that I have to specify the lifetime (not that I would have really grokked that yet). I'm trying my best to do so but Rust is never happy with what I come up with. Take a look at my inline comments for the compile errors I got. struct Floor{ handler: Option<|| ->&str> //this gives: missing lifetime specifier //handler: Option<||: 'a> // this gives: use of undeclared lifetime name `'a` }

Borrow-check error with variable not living long enough in nested lambda

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 08:37:02
问题 I am getting an error inside a nested lambda. let rows = vec![ vec![3, 6, 2, 8, 9, 0], vec![0, 0, 1, 4, 5, 1], ]; let pair_sums = rows.iter() .flat_map(|row| { (0 ..= row.len()).map(|i| row[i] + row[i + 1]) }) .collect::<Vec<_>>(); println!("{:?}", pair_sums); error[E0597]: `row` does not live long enough --> src/main.rs:9:40 | 9 | (0..row.len() - 1).map(|i| row[i] + row[i + 1]) | --- ^^^ does not live long enough | | | capture occurs here 10 | }) | - borrowed value only lives until here 11 |