lifetime

How can I model a bidirectional map without annoying the borrow checker?

耗尽温柔 提交于 2019-12-09 16:17:56
问题 From Why can't I store a value and a reference to that value in the same struct? I learned that I cannot store a value and a reference in the same struct. The proposed solution is: The easiest and most recommended solution is to not attempt to put these items in the same structure together. By doing this, your structure nesting will mimic the lifetimes of your code. Place types that own data into a structure together and then provide methods that allow you to get references or objects

What does “Box<Fn() + Send + 'static>” mean in rust?

十年热恋 提交于 2019-12-09 09:45:14
问题 What does Box<Fn() + Send + 'static> mean in rust? I stumbled upon this syntax while reading advanced types chapter. Send is a trait but what does it mean to + a lifetime to a trait ( 'static in this case) in type parametrization ? Also what is Fn() ? 回答1: Let's decompose it one-by-one. Box Box<T> is a pointer to heap-allocated T . We use it here because trait objects can only exist behind pointers. Trait objects In Box<Fn() + Send + 'static> , Fn() + Send + 'static is a trait object type. In

Safe to pass pointer to auto variable to function?

不问归期 提交于 2019-12-09 05:14:01
问题 Suppose I have a function that declares and initializes two local variables – which by default have the storage duration auto . This function then calls a second function, to which it passes the addresses of these two local variables. Can this second function safely use these pointers? A trivial programmatic example, to supplement that description: #include <stdio.h> int adder(int *a, int *b) { return *a + *b; } int main() { auto int a = 5; // `auto' is redundant; included for clarity auto

How do I specify a lifetime that is dependent on the borrowed binding of a closure in a separate type?

人盡茶涼 提交于 2019-12-08 20:15:16
问题 I have two types: Lexer and SFunction . SFunction stands for stateful function and is definined like so: struct SFunction { f: Option<Box<FnMut() -> SFunction>>, } The important part is that any SFunction references a closure that returns an SFunction . Now I want to have these functions carry state by each affecting the same Lexer . This means that each of these SFunctions has to have a lifetime that depends on a specific Lexer . Here's some more code if you want to get more of a sense of

PHP - session_set_cookie_params(), lifetime doesn't work

China☆狼群 提交于 2019-12-08 06:45:57
问题 As first, I know a lot questions are like mine, but I really don't know what I'm doing wrong... As you might've guessed, I've a PHP script involving sessions. Everything works like a charm, except setting the lifetime of my session. I want to keep the session active for two weeks, but instead my (Chrome) browser says it's set to xpire after the browsing session (and it does). My PHP script: session_name('DSWLogin'); // Naming the session session_set_cookie_params(2*7*24*60*60); // Making the

Rust lifetime error

匆匆过客 提交于 2019-12-08 05:45:12
问题 Can anyone tell what the lifetime error is in the following code? (simplified from my actual code) I've looked it over myself, but I can't figure out what is wrong or how to fix it. The problem comes when I try to add the Cell , but I'm not sure why. use std::cell::Cell; struct Bar<'a> { bar: &'a str, } impl<'a> Bar<'a> { fn new(foo: &'a Foo<'a>) -> Bar<'a> { Bar{bar: foo.raw} } } pub struct Foo<'a> { raw: &'a str, cell: Cell<&'a str>, } impl<'a> Foo<'a> { fn get_bar(&self) -> Bar { Bar::new(

Passing mutable context into callbacks

回眸只為那壹抹淺笑 提交于 2019-12-07 16:39:39
问题 I'm trying to build a simple UI in Rust, but partly scriptable in Lua, using rust-lua53, and having problems working out a good way to give Lua components access to the Lua state. This question/example is a little longer than I'd like, sorry! The core of the UI is a Widget trait with methods to call when keys are pressed or when the screen/window should be redrawn. The type parameter C is the context I'll want to pass in (see later). trait Widget<C> { fn handle_key<'c>(&mut self, key: char,

How do I update a variable in a loop to a reference to a value created inside the loop?

ⅰ亾dé卋堺 提交于 2019-12-07 12:53:49
问题 I want to enter a loop with a variable n which is borrowed by the function. At each step, n takes a new value; when exiting the loop, the job is done, with the help of other variables, and n will never be used again. If I don't use references, I have something like this: fn test(n: Thing) -> usize { // stuff let mut n = n; for i in 1..10 { let (q, m) = n.do_something(...); n = m; // stuff with x } x } x is the result of some computation with q and m but it is an usize type and I didn't

Destructor called on assignment between (stack) variables?

半世苍凉 提交于 2019-12-07 08:42:51
问题 matrix m1(5,5); matrix m2(5,5); m1 = matrix(m2); For the above code (for an arbitrary class, matrix), will the destructor be called for the information associated with m1, when the information of m2 is copied over to it? 回答1: No, an assignment operator will need to deal with releasing whatever resources m1 may hold before performing an assignment. The destructor will be called only when m1 is about to go out of scope. 回答2: No, once an object which is allocated on the stack is constructed it

How do lifetime bounds on structs work in Rust?

折月煮酒 提交于 2019-12-07 08:25:29
问题 There was some discussion of this in IRC yesterday, which left me feeling vaguely dissatisfied. The question was: How do you define a lifetime on a struct to restrict its contents to only things that live as long as 'itself'. i.e. a 'self sort of thing. My initial reaction was: you can't. If you create a struct Foo<'a>, the lifetime associated with it is inferred from the references it contains; unless the struct contains a reference to itself (impossible), you can't have this sort of 'self