Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend lifetime of variable

Tags:

rust

lifetime

I'm trying to return a slice from a vector which is built inside my function. Obviously this doesn't work because v's lifetime expires too soon. I'm wondering if there's a way to extend v's lifetime. I want to return a plain slice, not a vector.

pub fn find<'a>(&'a self, name: &str) -> &'a[&'a Element] {
    let v: Vec<&'a Element> = self.iter_elements().filter(|&elem| elem.name.borrow().local_name == name).collect();
    v.as_slice()
}
like image 375
SBSTP Avatar asked Mar 02 '15 22:03

SBSTP


People also ask

How can you extend the lifetime of an object?

The lifetime of a temporary object may be extended by binding to a const lvalue reference or to an rvalue reference (since C++11), see reference initialization for details.

What is meant by lifetime of a variable?

The lifetime of a variable defines the duration for which the computer allocates memory for it (the duration between allocation and deallocation of memory). In C language, a variable can have automatic, static or dynamic lifetime. Automatic − A variable with automatic lifetime are created.

What is the lifetime of an object?

In object-oriented programming (OOP), the object lifetime (or life cycle) of an object is the time between an object's creation and its destruction.


1 Answers

You can't forcibly extend a value's lifetime; you just have to return the full Vec. If I may ask, why do you want to return the slice itself? It is almost always unnecessary, since a Vec can be cheaply (both in the sense of easy syntax and low-overhead at runtime) coerced to a slice.

Alternatively, you could return the iterator:

use std::iter;

pub fn find<'a>(&'a self, name: &str) -> Box<Iterator<Item = &'a Element> + 'a> {
    Box::new(self.iter_elements()
       .filter(move |&elem| elem.name.borrow().local_name == name))
}

For now, you will have to use an iterator trait object, since closure have types that are unnameable.

NB. I had to change the filter closure to capture-by-move (the move keyword) to ensure that it can be returned, or else the name variable would just passed into the closure pointer into find's stack frame, and hence would be restricted from leaving find.

like image 78
huon Avatar answered Sep 29 '22 08:09

huon