Questions tagged [lifetime-scoping]
97 questions
0
votes
1 answer
How to express lifetime bounds in Rust when using references in trait methods
Here is a code snippet illustrating a lifetimes issue I ran into:
use std::collections::HashSet;
pub trait Store {
fn put<'a, 'b: 'a>(&mut self, val: &'b str);
}
struct Hstore<'a> {
hset: HashSet<&'a str>,
}
impl<'a> Store for Hstore<'a>…

mattgathu
- 1,129
- 1
- 19
- 28
0
votes
2 answers
rust "borrowed data escapes outside of method" with closure
The system here is:
event: a trait to be "extended" for different types of events
window: controller responsible for window creation and propagating its events further up the queue
application: controller of the whole application which creates a…

Vladislav
- 33
- 4
0
votes
1 answer
Why does this static life time doesn't live long enough?
I have the following code which I would expect to work.
let mut some_str: &'static str = "Hello world";
let _borrow: &'static mut &'static str = &mut some_str;
However when I compile this I get:
--> src/lib.rs:26:50
|
25 | let mut…

Parker
- 351
- 2
- 9
0
votes
1 answer
Good return type for create function for a noncopyable nonmovable type which may fail
I habe a Type A which gets passed by const reference into a scope where I need to make some computations on it.
I have a whole group of utility functions that are associated to that type A and I wanted to bundle them up in a class that holds a…

Tolar
- 61
- 4
0
votes
0 answers
How do you express async callback lifetimes relative to another async function awaiting it?
I'm working on a database library and I have a situation I can't figure out how to express with Rust lifetimes. I have a transact method which does the following:
Read a record from the DB
Call a passed-in modify function
Commit the modified record…

Ogapo
- 551
- 5
- 9
0
votes
0 answers
How do I pass a variable to a function that then changes the variable depending on an if statement?
I do not understand how to properly set up this line of logic without my variables going out of scope. I am trying to create the below function chunk_file. My first if statement ensures that the passed file does exist and that it is to be chunked by…

SinfulFrench
- 11
- 3
0
votes
1 answer
Lifetimes in for loop
How can I specify lifetimes in a for loop? I'm trying to multithread ports, but keep running into lifetime issues.
pub fn connect_server(&self) {
for x in 58350..58360 {
thread::spawn(|| {
…

paspielka
- 15
- 3
0
votes
0 answers
DbContext as Scoped service does not complete the operation and throws exceptions
I have simple DbContext with interface which is registered as scoped service:
services.AddDbContext(cfg =>
{
cfg.UseSqlServer(configuration.GetConnectionString("Default"), cfg =>
{
…

Szyszka947
- 473
- 2
- 5
- 21
0
votes
1 answer
What is the Rust compiler saying here?
error[E0621]: explicit lifetime required in the type of `is`
--> src/bee.rs:76:45
|
75 | fn compute_instruction_set<'a>(is: &'a mut InstructionSet) {
| ---------------------- help: add explicit lifetime…

user1098300
- 43
- 5
0
votes
0 answers
How can I remove duplicates in HashMap values and replace them with references
My hashmap has a lot of duplicate values in it, and instead of leaving them as their own variables, I want to create another hashmap of every possible unique value, then in the original hashmap, it simply just references one of the values in the…

James Gaunt
- 119
- 1
- 14
0
votes
1 answer
For loop - Struct with lifetime 'a cannot borrow as mutable because it is also borrowed as immutable
I have a struct which maps ids to indices and vice versa.
struct IdMapping<'a> {
external_2_internal: HashMap<&'a str, usize>,
internal_2_external: HashMap,
}
impl<'a> IdMapping<'a> {
fn new() -> IdMapping<'a> {
…

Janek
- 3
- 3
0
votes
1 answer
Can a trait's impl specify a lifetime that comes from a method's input argument?
For a type
pub struct Child<'a> {
buf: &'a mut [u8],
}
I can define a trait and implement the trait for the type but with a lifetime that is bound to a calling function's context (not to a local loop context):
pub trait MakeMut<'a> {
fn…

WeakPointer
- 3,087
- 27
- 22
0
votes
1 answer
Microsoft DI - Are objects referenced within a factory implementation disposed?
Are objects that are referenced, not created, within a factory implementation disposed by the container? See code below:
services.AddTransient(c => OwinContext.ServiceObject);
Will ServiceObject, which implements IDisposable, be disposed by the…

AnimaSola
- 7,146
- 14
- 43
- 62
0
votes
1 answer
Returns a reference to str from !format
I´m following the rust's reference book and in the chapter 10.3. Validating References with Lifetimes I'm playing with various values and ways to wrasp the concepts rigth.
But i can't found a solution to return an &str with a diferent lifetime for…

waldo
- 1
0
votes
1 answer
C++ ensure object exists while executing a function
I have a function foo. During the execution of foo, I want to be certain that an object of type Bar exists. Let’s call whatever object that happens to be “bar.”
I cannot copy or move bar, and bar can have any storage duration. The one thing I do…

nebuch
- 6,475
- 4
- 20
- 39