2,805 questions
- Bountied 0
- Unanswered
- Frequent
- Score
- Trending
- Week
- Month
- Unanswered (my tags)
Best practices
0
votes
2
replies
95
views
Can I have a reference to a variable while still allowing that variable to be mutated in Rust?
Is there a way that I can have a reference to a variable while allowing that variable to be mutated in Rust, so long as I "pinky promise not to access the data of the stored variable" so ...
3
votes
1
answer
80
views
Borrowed value does not live long enough for generic lifetime
The Rust compiler gives me a lifetime error with the following code (which is a minimal working example of some real code):
pub trait TElement: Copy {
// Details irrelevant
}
pub trait TSet<'a,...
5
votes
2
answers
149
views
Rust global variable lifetime safety
This is a pattern I want to be able to use in my code to have global variables that contain non-static references. Is it memory safe? If not, how can I make it so?
use std::cell::Cell;
pub struct ...
11
votes
1
answer
601
views
Why does std::thread::Scope::spawn need the bound `T: 'scope`?
The signature of std::thread::Scope::spawn looks like this:
pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
where
F: FnOnce() -> T + Send + 'scope,
...
Best practices
0
votes
3
replies
67
views
Most efficient way to manage the lifecycle of HttpClient in many wrapper classes?
Our application is somewhat of a middleman that transforms data from numerous APIs. Initially to simplify things, we wrote a bunch of specific clients which wrap an instance of HttpClient created in ...
3
votes
1
answer
139
views
Why is a mutable reference with any lifetime not enough to make a type invariant in a lifetime parameter?
While trying to convince myself precisely why this code in std::thread::Scope works, I hit upon the following problem. Below is a struct that accepts two lifetime parameters, and a pair of functions ...
0
votes
1
answer
58
views
Lifelines KM with CI ribbon does not show correctly with plotnine
Using default code
from lifelines.datasets import load_dd
data = load_dd()
kmf = KaplanMeierFitter()
kmf.fit(data["duration"], event_observed=data["observed"])
kmf....
5
votes
1
answer
138
views
Rust specifying a trait and associated type are valid for the lifetime inside a function
I am using the winnow crate to write a parser and am struggling with
adding tests due to lifetime conflicts.
The basic setup I have is a stateful stream:
// The primary stream type
type SStream<'i, ...
5
votes
0
answers
94
views
Drop Order Changes Randomly Between Async Boundaries
I noticed that destructors (Drop) in async functions don’t always execute in the same order when nested awaits are used.
struct Guard(&'static str);
impl Drop for Guard { fn drop(&mut self) { ...
0
votes
1
answer
145
views
Why const reference could not extend lifetime of temporary
After reading this, I got an idea, What if I make g_map a const reference, which should extend the life time of the temporary object. So it can be valid until end of main function. But it seems that ...
0
votes
2
answers
166
views
Does this approach to examine object representation reuse storage?
In order to examine object representation, the C++ standard provides https://timsong-cpp.github.io/cppwp/n4861/basic.lval#11:
If a program attempts to access ([defns.access]) the stored value of an ...
1
vote
1
answer
120
views
Why does 'borrowed valued does not live long enough' error appear when storing value and its reference in a struct?
I'm aware it is not much possible nor recommended to have both a field that owns a value, and another one that stores a reference to the same value in a struct.
I was experimenting a bit with a more ...
21
votes
2
answers
541
views
Share buffer during a loop
Just stumble across Why we didn't rewrite our feed handler in Rust from databento
And they say they cannot share a buffer during a loop. What would be the rust way to fix this without making copies of ...
11
votes
1
answer
168
views
How to fix lifetimes when returning and iterator over borrowed data?
I'm developing a rust crate that abstracts audio buffers. A key challenge are non continuous slices.
My solution to those is the MutableView, it's supposed to remap a continuous index to a non ...
14
votes
1
answer
487
views
Can a Box<dyn Any> have a lifetime less than 'static?
In Rust, the object Box<dyn Any> has a default life time of 'static. The compiler seems to accept using a different lifetime, for example Box<dyn Any + 'short>. However, this causes a ...