lifetime Questions

3

Solved

Rust supports trait inheritance, as follows: pub trait A {} pub trait B: A {} B: A means that if some type T implements B, it also needs to implement all the methods in A. But today I see the foll...
Bucky asked 10/11, 2021 at 8:59

2

Solved

I was reviewing a code I proposed to initialize a std::array at compile-time for non-default-constructible objects: https://mcmap.net/q/905632/-idiom-for-initializing-an-std-array-using-a-generator...

1

Solved

Consider the toy example code below. It has a too-large-to-copy data structure MyDataStructure and a little iterator class MyDataIterator that the caller can instantiate to iterate over the data in...
Birdsong asked 30/9 at 23:48

0

I can't figure out why I need a 'static bound in one case (bar) and not the other (baz): fn f<T>(_input: T) -> bool { false } fn bar<T>() -> Box<dyn Fn(T) -> bool + 'stati...
Farreaching asked 8/9 at 15:36

1

Consider the following rust program: fn f<'a>(x: &'a i32) { unimplemented!(); } fn main() { f::<'static>; } When compiling it, the following compilation error is outputed: error...
Digamma asked 21/2, 2021 at 12:57

4

Solved

Consider the following example from the Book: fn main() { let string1 = String::from("abcd"); let string2 = "xyz"; let result = longest(string1.as_str(), string2); println!...
Discrepant asked 2/6, 2021 at 9:32

2

Solved

The third rule of lifetime elision says If there are multiple input lifetime parameters, but one of them is &self or &mut self because this is a method, then the lifetime of self is assi...
Recidivate asked 20/9, 2017 at 23:36

1

I think I understood how lifetimes work with function parameters/outputs and with structs, since those cases are explained in the book (and further in the nomicon), but I do not understand how life...
Evangelize asked 13/5, 2020 at 19:50

5

Solved

In vulkano, to create a CPUAccessibleBuffer you need give it some data and the CPUAccessibleBuffer::from_data function requires the data to have the 'static lifetime. I have some data in &[u8] ...
Angelika asked 8/11, 2021 at 18:10

1

The Rust book provides the following code to illustrate a valid function definition where explicit lifetimes are required: fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { ...
Hydrochloride asked 2/5 at 17:49

1

Solved

I'd like to make the following Padded type into an iterator transformer: enum Step<T> { Before(T), During(T), After } struct Padded<T> { step: Step<T> } (Note that in my rea...
Taciturnity asked 8/1 at 13:56

2

Solved

I'm having some trouble trying to grasp why I can't return an &str value generated from a String (goodness, when will as_str be ready?) and I'm doing something wrong. I get this idea because no...
Oxalate asked 21/4, 2015 at 19:24

5

Solved

Consider this code: const char* someFun() { // ... some stuff return "Some text!!" } int main() { { // Block: A const char* retStr = someFun(); // use retStr } } In the function someFun()...
Grubb asked 5/4, 2010 at 17:32

1

I am trying to return a struct containing reference to a shared mutex: struct Test<'a> { mutex: Arc<Mutex<()>>, guard: &'a MutexGuard<'a, ()>, } impl<'a> Test&l...
Lukewarm asked 21/1, 2022 at 19:42

4

Solved

I want to write this structure: struct A { b: B, c: C, } struct B { c: &C, } struct C; The B.c should be borrowed from A.c. A -> b: B -> c: &C -- borrow from --+ | c: C <...
Silvey asked 21/12, 2014 at 11:47

3

Solved

There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifeti...
Klinges asked 6/9, 2016 at 18:54

1

Solved

P0593, under the Type punning section, presents this example: float do_bad_things(int n) { alignof(int) alignof(float) char buffer[max(sizeof(int), sizeof(float))]; *(int*)buffer = n; // #1 new...
Firewater asked 7/9, 2023 at 10:43

1

Solved

This odd bit of code emits an error from do_the_thing() saying T does not live long enough: use std::future::Future; trait Connection: Send { type ExecFut<'a>: Future<Output = ()> + S...
Taunt asked 4/9, 2023 at 0:45

1

Solved

From my own understanding and experimentation this appears to be true, but I have yet to find an authoritative source that documents it. Rust by Example has a bounds section where it says: T: 'a: ...
Dutiful asked 1/9, 2023 at 18:23

4

Solved

The following C++ code prints 11.1 then crashes. The lambda function seems to be called correctly inside the constructor, but then later, that same function no longer works! Why is this happening? ...
Hodgkins asked 1/8, 2023 at 23:26

2

Solved

After P0593R6 ('Implicit creation of objects for low-level object manipulation') was accepted in C++20, C++23 will get std::start_lifetime_as() which 'completes the functionality proposed in [P0593...
Shadshadberry asked 10/6, 2023 at 10:43

1

Solved

At Lifetime under Providing storage, it says: As a special case, objects can be created in arrays of unsigned char or std::byte (since C++17) (in which case it is said that the array provides stor...
Tithing asked 20/7, 2023 at 4:33

3

Solved

I have an async method that should execute some futures in parallel, and only return after all futures finished. However, it is passed some data by reference that does not live as long as 'static (...
Heathenry asked 12/12, 2020 at 20:56

4

Solved

In Rust, when we want a struct to contain references, we typically define their lifetimes as such: struct Foo<'a> { x: &'a i32, y: &'a i32, } But it's also possible to define mul...
Sidesaddle asked 25/4, 2015 at 5:29

11

Solved

I was reading the lifetimes chapter of the Rust book, and I came across this example for a named/explicit lifetime: struct Foo<'a> { x: &'a i32, } fn main() { let x; // -+ x goes into...
Modlin asked 24/7, 2015 at 11:15

© 2022 - 2024 — McMap. All rights reserved.