lifetime

Prevent Android activity from being recreated on turning screen off

随声附和 提交于 2019-11-28 10:51:03
How to prevent an activity from being recreated on turning screen off? What I do Start Bejewels and go to the jewelry screen. Press power button shortly. The screen is turned off, but the device is not. Press power button again. What I see The same screen as before turning screen off. In case of my application (trivial one, just a web-app with a single WebView) the scenario is the following: What I do Start my app. The activity onCreate() method loads an URL into the WebView. Press power button shortly. The screen is turned off, but the device is not. Press power button again. What I see The

Getting 'Missing Lifetime specifier' error

六月ゝ 毕业季﹏ 提交于 2019-11-28 10:13:52
问题 This is my code: use std::ptr; struct Query<T>{ data: T, prev: & Query<T>, next: & Query<T>, } impl<T> Query<T>{ fn new(name: T) -> Query<T>{ Query{ data: name, prev: ptr::null(), next: ptr::null(), } } } I keep getting 'missing lifetime specifier' where I am referencing &Query<T> . How do I fix this error? 回答1: First, note that &T / &mut T and *const T / *mut T are different types of pointers in Rust. The former are called "references" and they are statically checked in various ways

How do I make format! return a &str from a conditional expression?

送分小仙女□ 提交于 2019-11-28 10:11:22
问题 I happened upon this problem where format! creates a temporary value in a pattern that is not anchored to anything, as far as I understand it. let x = 42; let category = match x { 0...9 => "Between 0 and 9", number @ 10 => format!("It's a {}!", number).as_str(), _ if x < 0 => "Negative", _ => "Something else", }; println!("{}", category); In this code, the type of category is a &str , which is satisfied by returning a literal like "Between 0 and 9" . If I want to format the matched value to a

Extend lifetime of a variable for thread

心不动则不痛 提交于 2019-11-28 10:08:27
问题 I am reading a string from a file, splitting it by lines into a vector and then I want to do something with the extracted lines in separate threads. Like this: use std::fs::File; use std::io::prelude::*; use std::thread; fn main() { match File::open("data") { Ok(mut result) => { let mut s = String::new(); result.read_to_string(&mut s); let k : Vec<_> = s.split("\n").collect(); for line in k { thread::spawn(move || { println!("nL: {:?}", line); }); } } Err(err) => { println!("Error {:?}",err);

How should I restructure my graph code to avoid an “Cannot borrow variable as mutable more than once at a time” error?

醉酒当歌 提交于 2019-11-28 09:55:40
问题 I have a simple graph that successfully compiles: use std::collections::HashMap; type Key = usize; type Weight = usize; #[derive(Debug)] pub struct Node<T> { key: Key, value: T, } impl<T> Node<T> { fn new(key: Key, value: T) -> Self { Node { key: key, value: value, } } } #[derive(Debug)] pub struct Graph<T> { map: HashMap<Key, HashMap<Key, Weight>>, list: HashMap<Key, Node<T>>, next_key: Key, } impl<T> Graph<T> { pub fn new() -> Self { Graph { map: HashMap::new(), list: HashMap::new(), next

Does <'a, 'b: 'a> mean that the lifetime 'b must outlive the lifetime 'a?

戏子无情 提交于 2019-11-28 09:47:53
I want to implement a builder similar to the debug builders defined by the standard library. They are defined using structures like the following: struct DebugFoo<'a, 'b: 'a> { fmt: &'a mut std::fmt::Formatter<'b> } Since I don't understand what the form <'a, 'b: 'a> means nor I can find it mentioned in the Rust book or the Rust reference (at least concerning lifetimes), I just tried to remove what I don't understand to see what happens: struct DebugFoo<'a, 'b> { fmt: &'a mut std::fmt::Formatter<'b> } Compiling it I get this error: in type `&'a mut core::fmt::Formatter<'b>`, reference has a

How to return a future combinator with `&self`

喜欢而已 提交于 2019-11-28 08:35:16
问题 I have this piece of code: impl ArcService for (Box<MiddleWare<Request>>, Box<ArcService>) { fn call(&self, req: Request, res: Response) -> Box<Future<Item = Response, Error = Error>> { box self.0.call(req).and_then(move |req| self.1.call(req, res)) } } pub trait ArcService: Send + Sync { fn call(&self, req: Request, res: Response) -> Box<Future<Item = Response, Error = Error>>; } pub trait MiddleWare<T>: Sync + Send { fn call<'a>(&'a self, param: T) -> Box<Future<Item = T, Error = Error> +

How do I create an array of unboxed functions / closures?

徘徊边缘 提交于 2019-11-28 08:18:31
问题 Editor's note: This question was asked before Rust 1.0 and some of the syntax has changed since then, but the underlying concepts remain. Some answers have been updated for Rust 1.0 syntax. I'm new to Rust and trying to do something with closures which is trivial in JavaScript, Python, etc. but I am running into lifetime issues in Rust. I understand the error message, but it leads me to believe what I want to do is pretty hard in Rust. I just want to create an array of functions, a , such

How to initialize a variable with a lifetime?

佐手、 提交于 2019-11-28 07:16:27
问题 I have following code and don't know how to get it working: fn new_int<'a>() -> &'a isize { &5 } fn main() { let x = new_int(); } Or another attempt: fn new_int<'a>() -> &'a isize { let a: &'a isize = &5; a } fn main() { let x = new_int(); } 回答1: You can't. A lifetime parameter does not allow you to choose how long a value lives, it only allows you to communicate to the compiler that two or more references are "related" to the same memory and are expected to share the same lifetime. A

Explicit lifetime declarations in trait objects held by structs

*爱你&永不变心* 提交于 2019-11-28 04:29:23
问题 In the answer to this question there's a discussion of how to refer to trait objects held by structs which requires the following syntax: struct Bar<'a> { foo: &'a (Foo + 'a), } This is per RFC 438 Could I ask for more explanation of the double lifetime declaration? Levans said: You have to specify the lifetime two times : once for the lifetime of the reference, and once for the trait object itself, because traits can be implemented for references, and if the underlying object is a reference,