lifetime Questions

1

Consider the following methods (fiddle): void test(Span<int> param) { //Fail, the stackalloc'ed buffer could be exposed. param = stackalloc int[10]; } void test2(Span<int> param) { ...
Friarbird asked 28/4, 2021 at 11:59

1

I am having trouble passing additional state to my service function, but I can't sort out the lifetimes in the closures. None of the tutorials seem to address it: https://hyper.rs/ https://docs.r...
Preece asked 2/4, 2020 at 16:31

2

Solved

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...
Couture asked 29/12, 2017 at 4:10

1

Solved

I understand what it means for a borrow, trait, or struct to have a lifetime, but it doesn't make sense to me why a type would have one. My understanding of types is that they are an abstraction th...
Batiste asked 3/4, 2021 at 22:6

1

Solved

Arrays of any type are implicit-lifetime objects, and it is possible to to begin the lifetime of implicit-lifetime object, without beginning the lifetime of its subobjects. As far as I am aware, th...
Grommet asked 21/3, 2021 at 10:13

2

I have a strange piece of code: #![allow(unused)] fn f<'a>() {} fn g<'a: 'a>() {} fn main() { // let pf = f::<'static> as fn(); // (7) let pg = g::<'static> as fn(); // ...
Mews asked 2/1, 2021 at 2:45

1

After reading the rust book many times I think I'm starting to get the gist of lifetimes, but for me, an additional problem is the syntax we need to use to declare them. I find it is really counter...
Clarissa asked 13/3, 2021 at 0:46

2

Solved

I have found a lot of information across the web about rust lifetimes, including information about static lifetimes. It makes sense to me that, in certain situations, you must guarantee that a refe...
Craftwork asked 6/3, 2021 at 20:45

1

Solved

struct Foo01<'a> { val: u32, str: &'a String, } fn mutate_and_share_01<'a>(foo: &'a mut Foo01<'a>) -> &'a Foo01<'a> { foo } fn mutate_and_share_02<'a&...
Photoengrave asked 10/2, 2021 at 8:20

1

Solved

Please consider the following example (playground): struct Animal<'a> { format: &'a dyn Fn() -> (), } impl <'a>Animal<'a> { pub fn set_formatter(&mut self, _fmt: &am...
Duky asked 31/1, 2021 at 22:49

1

Solved

Let's say I want to write code like this: struct Inspector<'a>(&'a u8); struct Foo<'a> { value: Box<u8>, inspector: Option<Inspector<'a>>, } fn main() { let m...
Gave asked 24/1, 2021 at 2:18

1

Solved

I wrote the code below, but I can't write life time constraint to work and get an error: use futures::Future; async fn foo<'a>(a: &'a str) -> &'a str { let task = get(); f(a, ta...
Hollishollister asked 18/1, 2021 at 6:30

0

CWG 1815 asked (with minor edits): struct A {}; struct B { A&& a = A{}; }; B b1; // #1 B b2{A{}}; // #2 B b3{}; // #3 [...] #2 is aggregate initialization, which binds B::a to the tempora...
Cassidy asked 1/1, 2021 at 9:59

1

Solved

I am trying to understand what T: 'a means, where, I guess, T is a type and 'a is a lifetime. I understand what 'a: 'b means: x('a) outlives y('b) and so we cannot assign y to x in the following co...
Esparza asked 31/12, 2020 at 11:5

1

Solved

When I wondered how a mutable reference could move into a method, all the questions began. let a = &mut x; a.somemethod(); // value of a should have moved a.anothermethod(); // but it works. I...
Synecdoche asked 28/12, 2020 at 7:19

2

Solved

When we write: fn foo<'a, 'b>(x: &'a u32, y: &'b u32) -> &'a u32 { x } Why don't we just refer to the 'as and the 'bs as lifetime parameters as opposed to generic lifetime pa...
Licking asked 17/12, 2020 at 23:32

2

Solved

While reading Chapter 12.4 of the Rust Book, I stumbled upon this function: pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { vec![] } I understand wh...
Synge asked 3/12, 2020 at 14:2

3

Solved

fn main() { let strA = "a"; let result; { let strB = "abc"; result = longest(strA, strB); // Will return strB } println!("The longest string is {}", res...
Supraorbital asked 27/11, 2020 at 15:46

0

I was reading the lifetime safety core guidelines paper and was eager to try the -Wlifetime flag in practice. For starters, I wrote the following simple example: class Wrapper { public: Wrapper(st...
Kronick asked 8/11, 2020 at 20:21

1

The issue: I'm getting a "XXXXXXX dropped here while still borrowed" error from a method where XXXXXXX is valid to the end of the method (which is fine), but Rust is unnecessarily expecti...
Ulent asked 25/10, 2020 at 5:26

2

Solved

fn main() { let _ref_in_ref_out = |var: &i64| var; } This does not compile: error: lifetime may not live long enough --> src/main.rs:2:39 | 2 | let _ref_in_ref_out = |var: &i64| var;...
Lemuroid asked 11/9, 2020 at 9:0

1

In the following short example, what can be said about the object the pointer f points to or used to point to just before returning from main? #include <vector> struct foo { std::vector<...
Commissar asked 7/9, 2020 at 1:43

2

From https://timsong-cpp.github.io/cppwp/basic.compound#3 : Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or ...
Picnic asked 7/9, 2020 at 0:50

4

Solved

In C++, is this code correct? #include <cstdlib> #include <cstring> struct T // trivially copyable type { int x, y; }; int main() { void *buf = std::malloc( sizeof(T) ); if ( !buf...
Quilting asked 8/5, 2015 at 1:35

2

Solved

I was making the executor/reactor while discovered this a lifetime problem. It is not related to async/Future and can be reproduced without async sugar. use std::future::Future; struct Runtime; f...
Eurasian asked 21/8, 2020 at 5:55

© 2022 - 2024 — McMap. All rights reserved.