lifetime

Function references: expected bound lifetime parameter , found concrete lifetime [E0271]

巧了我就是萌 提交于 2019-12-10 19:17:16
问题 There are already a lot of threads on this topic but I fail to see if the discussed problems apply to my specific problem. I have a structure that stores a name and a callback function. Stripped down to the problem it looks like this: pub struct Command<'a> { name: &'a str, callback: &'a Fn(&[&str]) -> () } impl <'a> Command<'a> { pub fn new(name: &'a str, callback: &'a Fn(&[&str]) -> ()) -> Command<'a> { Command { name: name, callback: callback } } } What I want to do is store a callback

Lifetime parameters for an enum within a struct

孤街醉人 提交于 2019-12-10 17:44:17
问题 I don't understand why I get an error with this type of structure enum Cell <'a> { Str(&'a str), Double(&'a f32), } struct MyCellRep<'a> { value: &'a Cell, ptr: *const u8, } impl MyCellRep{ fn new_from_str(s: &str) { MyCellRep { value: Cell::Str(&s), ptr: new_sCell(CString::new(&s)) } } fn new_from_double(d: &f32) { MyCellRep { value: Cell::Double(&d), ptr: new_dCell(&d) } } } I get the error 14:22 error: wrong number of lifetime parameters: expected 1, found 0 [E0107] src\lib.rs:14 value : &

Rust: “cannot move out of `self` because it is borrowed” error

那年仲夏 提交于 2019-12-10 16:59:31
问题 I'm trying to write a recursive method that adds an item to a tree and returns the tree node corresponding to that item. enum BstNode { Node(int, ~BstNode, ~BstNode), Leaf } impl BstNode { fn insert<'a>(&'a mut self, item: int) -> &'a mut BstNode { match *self { Leaf => { *self = Node(item, ~Leaf, ~Leaf); self }, Node(ref node_item, ref mut left, ref mut right) => match item.cmp(node_item) { Less => left.insert(item), Equal => self, Greater => right.insert(item) } } } } I'm bitten by the

When returning the outcome of consuming a StdinLock, why was the borrow to stdin retained?

限于喜欢 提交于 2019-12-10 16:16:08
问题 Given the following function: use std::io::{BufRead, stdin}; fn foo() -> usize { let stdin = stdin(); let stdinlock = stdin.lock(); stdinlock .lines() .count() } This fails to compile with the following error: error: `stdin` does not live long enough --> src/main.rs:12:1 | 7 | let stdinlock = stdin.lock(); | ----- borrow occurs here ... 11 | } | ^ `stdin` dropped here while still borrowed | = note: values in a scope are dropped in the opposite order they are created I find this surprising

Borrowing reference and guard together from a Mutex

浪子不回头ぞ 提交于 2019-12-10 15:47:40
问题 I'm trying to encapsulate some code to avoid repeating it, relating to borrowing data out of a Mutex and further operations thereupon (which I leave off of this question as out of scope, but are the motivating factors). The following sample code complains that guard does not live long enough. But that is precisely why I'm returning guard in the structure designed expressly for that purpose. Is this a limitation of the borrow checker? Any suggestions on working around this? use std::sync::

Wrong inferred lifetime due to associated type

别来无恙 提交于 2019-12-10 13:45:42
问题 The following code sample is a minified version of a problem I have. trait Offset: Default {} trait Reader { type Offset: Offset; } impl Offset for usize {} impl<'a> Reader for &'a [u8] { type Offset = usize; } // OK // struct Header<R: Reader>(R, usize); // Bad struct Header<R: Reader>(R, R::Offset); impl <R: Reader<Offset=usize>> Header<R> { fn new(r: R) -> Self { Header(r, 0) } } fn test<R: Reader>(_: Header<R>, _: Header<R>) {} fn main() { let buf1 = [0u8]; let slice1 = &buf1[..]; let

Unity: pass parameters to custom lifetime constructor, in xml configuration file

南笙酒味 提交于 2019-12-10 11:17:58
问题 I wrote my CustomLifetimeManager like this: public class CustomLifetimeManager <T> : LifetimeManager { private readonly string _arg; public CustomLifetimeManager(string arg) { _arg = arg; } } Now, it works easy configuring the container programmatically, but how add it in configuration file like the following? <type type="myTime" mapTo="myImpl"> <lifetime type="CustomLifetimeManager"/> </type> 回答1: You need to add a second class: A TypeConverter. This class is responsible for taking a string

type parameter for function vs struct (lifetime issue)

萝らか妹 提交于 2019-12-10 09:53:51
问题 Consider the following test case: #![allow(unstable)] trait Choose<'o> { fn choose(a: &'o u64, b: &'o u32) -> Self; } impl<'o> Choose<'o> for &'o u64 { fn choose(a: &'o u64, _b: &'o u32) -> &'o u64 { a } } impl<'o> Choose<'o> for &'o u32 { fn choose(_a: &'o u64, b: &'o u32) -> &'o u32 { b } } // ' struct Handler { a: u64, b: u32, } impl Handler { fn new() -> Handler { Handler { a: 14, b: 15 } } fn find<'a, V, W>(&'a mut self, value: W) -> Option<V> where V: Choose<'a>, W: PartialEq<V> { // '

Default mutable value from HashMap

ぃ、小莉子 提交于 2019-12-10 02:38:38
问题 Suppose I have a HashMap and I want to get a mutable reference to an entry, or if that entry does not exist I want a mutable reference to a new object, how can I do it? I've tried using unwrap_or() , something like this: fn foo() { let mut map: HashMap<&str, Vec<&str>> = HashMap::new(); let mut ref = map.get_mut("whatever").unwrap_or( &mut Vec::<&str>::new() ); // Modify ref. } But that doesn't work because the lifetime of the Vec isn't long enough. Is there any way to tell Rust that I want

Expanding Rust Lifetime

大兔子大兔子 提交于 2019-12-10 01:37:01
问题 I have a bit of code that I'm fighting with. It's a little helper function that should return a Vec<&str> to the calling function. I can't seem to get the lifetime right, though. Here is the code snippet: fn take_symbol<'a>(ch: &'a str, current: &'a mut String) -> &'a mut TokenList<'a> { let out = TokenList::<'a>::new(); out.push(current.as_str()); out.push(ch); *current = String::new(); &mut out } The compiler is telling me: error: 'out' does not live long enough and that the reference must