Questions tagged [lifetime]

A variable's life-time is the time during which the variable is bound to a specific memory location. The life time starts when the variable is allocated and ends when it is deallocated.

Most of the time a lifetime is a synonym for the scope, for example in this code:

void foo()
{
    int x = 0; // lifetime of `x' begins here ──┐
    //                                          │
    printf("%d\n", x); //                       │
} // and finishes here ─────────────────────────┘

Rust:

The lifetime is a key concept in : it is a construct the compiler (also called the ) uses to ensure all borrows are valid.

Links:

2138 questions
0
votes
1 answer

Kerberos TGT Behaviour

Fictive scenario: A User logs in and get a Kerberos TGT with a default Lifetime of 10 hours. After 5 hours the user get disabled/locked on the directory controller. What happens then? Can the user despite that he is disabled/locked access resources…
murratore
  • 171
  • 1
  • 3
  • 9
0
votes
1 answer

Lifetimes for implementing a trait's associated type with a reference

I have a trait with an associated type, but I want to implement it with a reference type, however I can't get the lifetimes right. For example, without references, suppose I have: trait DoesStuff { type Output; fn do_thing(&mut self, x:…
Corvus
  • 7,548
  • 9
  • 42
  • 68
0
votes
0 answers

Rust, Attach the lifetime of a closure in a constructor to the object instead of the cosntructor?

Imagine you want to make an object that constructs s closure and becomes its owner, so that it can issue handles to it, this is MWE I managed to make: struct A<'a> { data: &'a usize, function: Box usize>, b:…
Makogan
  • 8,208
  • 7
  • 44
  • 112
0
votes
1 answer

Rust Reborrowing Shared Reference from Mutable Reference

I'm new to Rust, and trying to understand why a mutable reference to a data structure that has had an element be borrowed can seemingly be used again within the same lifetime to borrow another element. This is namely in regards to an example from an…
0
votes
0 answers

Rust: struct with vector of references to another vector of the struct

I defined two structs like these: #[derive(Debug)] struct Slices<'a> { intervals: &'a[u32] } #[derive(Debug)] struct NumbersWithSlices<'a> { numbers: Vec, slices: Vec> } and I managed to write a main like this: fn…
MaPo
  • 613
  • 4
  • 9
0
votes
0 answers

Regarding mutable and immutable borrow with lifetime and struct

I want to keep binary data in Struct(e.g., aout) and refer it partially using Slice(e.g., data) In the followings, I created a slice in test2 method and it might be succeeded. However, as show in the next block, I cannot invoke t.test3() I don't…
Hiro
  • 1
  • 1
0
votes
1 answer

PyQt crashes when QtItemDelegate subclass has two or more instances

I'm implementing a delegate for a QTableView where two columns should be dropdowns where the user selects a value from an enum. Below is an example: from PyQt4 import QtGui, QtCore import enum Color = enum.Enum("Color", ("RED", "BLUE")) Shape =…
Kaia
  • 862
  • 5
  • 21
0
votes
0 answers

Understanding mutable references, lifetimes, and borrowing in rust: issue with accessing references after passing to a function

fn main() { let mut s = String::from("hello"); let mut f = Foo(&mut s); let r = &mut f; run(r); println!("{:?}", f); // cannot borrow `f` as immutable ... // but println!("{}", s); works…
0
votes
0 answers

Correct definition of lifetime of a reference in Rust

I have a doubt about the definition of lifetime of a reference in Rust: the two definitions I've found in most articles are the following two: "The lifetime of a reference is the set of lines of code in which the reference is valid". "The lifetime…
0
votes
0 answers

A struct in Rust that contains two Vecs of sub-structs. One of the struct has a reference to the other one to make a link. How do i satisfy the BC?

Here is a made up example that reproduce the issue: struct Metro<'a> { stations: Vec>, monuments: Vec } struct Station<'a> { name: String, closest_monument: Option<&'a Monument> } struct Monument { …
doud
  • 1
0
votes
0 answers

Rust: Use borrowing Fn parameter for body owned variables

I have: a data producer (e.g. Rc>) generic function (e.g. fn assert_all(...)) which transform generated data (e.g. f) before being processed (e.g. assert_eq!(...)) : /// Pseudo-Rust code fn assert_all(producer: Rc>, f:…
LoganMzz
  • 1,597
  • 3
  • 18
  • 31
0
votes
0 answers

Why do I get "borrowed value does not live long enough" error?

I am writing a program in Rust that reads data from excel sheet and populates objects using that data. To convert each row of excel sheet into a Test object, I implemted From> for Test. My code looks like…
JUSEOK KO
  • 69
  • 7
0
votes
0 answers

How do I solve this lifetime problem in a struct with references to other data inside the same struct?

I have the following struct defs pub struct Connection { in_node_id: usize, out_node_id: usize, weight: f64, innovation_num: usize } pub struct Network<'a> { n_sensor_nodes: usize, n_output_nodes: usize, n_hidden_nodes:…
Chechy Levas
  • 2,206
  • 1
  • 13
  • 28
0
votes
2 answers

Is it really safe to return a string literal from a function?

Consider this code : char *test() { return "HELLO"; } int main() { char *p = test(); printf("%s\n", p); } This compiles without warning and I guess because "HELLO" is not stored in the stack. However this gives me a warning: char…
alessio solari
  • 313
  • 1
  • 6
0
votes
1 answer

Is there any way to cast a Callback into FnMut(MouseEvent)?

I'm implementing material 3 from scratch with web_sys and writing some adapters for different frameworks among them yew. I have implemented a basic structure for buttons something like this to use like this. This works fine on web_sys but I'm trying…
al3x
  • 589
  • 1
  • 4
  • 16