lifetime-scoping

Passing two objects, where one holds a reference to another, into a thread

自闭症网瘾萝莉.ら 提交于 2021-02-20 11:51:10
问题 I have two objects where the second one requires the fist one to outlive it because it holds a reference to the first one. I need to move both of them into a thread, but the compiler is complaining that the first one doesn't live long enough. Here is the code: use std::thread; trait Facade: Sync { fn add(&self) -> u32; } struct RoutingNode<'a> { facade: &'a (Facade + 'a), } impl<'a> RoutingNode<'a> { fn new(facade: &'a Facade) -> RoutingNode<'a> { RoutingNode { facade: facade } } } fn main()

What are non-lexical lifetimes?

不羁岁月 提交于 2021-02-10 16:42:34
问题 Rust has an RFC related to non-lexical lifetimes which has been approved to be implemented in the language for a long time. Recently, Rust's support of this feature has improved a lot and is considered complete. My question is: what exactly is a non-lexical lifetime? 回答1: It's easiest to understand what non-lexical lifetimes are by understanding what lexical lifetimes are. In versions of Rust before non-lexical lifetimes are present, this code will fail: fn main() { let mut scores = vec![1, 2

Variable lifetime

做~自己de王妃 提交于 2020-03-21 20:27:09
问题 What happends to variable when line of execution goes outside of code block? For example: 1 public void myMethod() 2 { 3 int number; 4 number = 5; 5 } so, we declare and set variable. When it goes outside of code block (line 5) what happends to variable number? Here is another example with creating instance of class: 7 public void myMethod() 8 { 9 Customer myClient; 10 myClient = new Customer(); 11 } When it goes outside of code block (line 11) what happends to object reference myClient? I

What are non-lexical lifetimes?

一世执手 提交于 2019-12-24 19:37:10
问题 Rust has an RFC related to non-lexical lifetimes which has been approved to be implemented in the language for a long time. Recently, Rust's support of this feature has improved a lot and is considered complete. My question is: what exactly is a non-lexical lifetime? 回答1: It's easiest to understand what non-lexical lifetimes are by understanding what lexical lifetimes are. In versions of Rust before non-lexical lifetimes are present, this code will fail: fn main() { let mut scores = vec![1, 2

DocumentDB client lifetime

孤街浪徒 提交于 2019-12-22 04:38:18
问题 To access DocumentDB/CosmosDB I'm using package Microsoft.Azure.DocumentDB.Core (v1.3.2). I have noticed when I create and initialise DocumentClient class: var documentClient = new DocumentClient(new Uri(endpointUrl), primaryKey); await documentClient.OpenAsync(); There is a number of requests fired to the endpoint to get information about indexes and other information. To be exact there are 9 HTTP requests going out on .OpenAsync() . This makes the creation and activation of the client a

When do I need to specify explicit lifetimes in Rust?

余生颓废 提交于 2019-12-18 19:43:55
问题 If I have the two functions // implicit fn foo(x: &i32) { } // explicit fn bar<'a>(x: &'a i32) { } When would foo return an error and bar be the correct function header? I'm confused as to why I would explicitly declare a lifetime: The 'a reads ‘the lifetime a’. Technically, every reference has some lifetime associated with it, but the compiler lets you elide them in common cases. I understand what a lifetime is, but what does explicitly specifying a lifetime 'a do for me? For reference I'm

Instance per matching lifetime scope, with default?

折月煮酒 提交于 2019-12-18 16:53:15
问题 I'd like to have an instance per matching lifetime scoped registration in Autofac, but occasionally need to request an instance from a global container (where there is no matching lifetime scope). In scenarios where no matching lifetime scope exists, I want to give a top-level instance instead of throwing an exception. Is this possible? 回答1: I think you'd better extend Autofac by introducing a new lifetime option. I took the Autofac sources and modified them a bit: public static class

The parameter type `T` may not live long enough when writing a binary searching tree

我只是一个虾纸丫 提交于 2019-12-12 06:53:16
问题 I'm trying to write a binary searching tree in Rust, but I don't understand what is going on: enum BST<'a, T: Ord> { Leaf, BinTree { value: T, left: &'a mut BST<'a, T>, right: &'a mut BST<'a, T> } } impl<'a, T: Ord> BST<'a, T> { fn new() -> BST<'a, T> { BST::Leaf } fn add(self, val: T) { match self { BST::Leaf => self = BST::BinTree { value: val, left: &mut BST::<'a, T>::new(), right: &mut BST::<'a, T>::new() }, BST::BinTree{value: v, left: l, right: r} => if val < v { l.add(val); } else { r

What is the point of an explicit lifetime for a method that doesn't take any arguments?

怎甘沉沦 提交于 2019-12-11 07:58:54
问题 On page 295 of Programming Rust you can find the following: Fortunately, the standard library includes the blanket implementation: impl<'a, T, U> AsRef<U> for &'a T where T: AsRef<U>, T: ?Sized, U: ?Sized, { fn as_ref(&self) -> &U { (*self).as_ref() } } I'm confused at the use of &'a there. What is the context of that? It's not being used in an argument of as_ref nor tied to the output of &U . I don't think I fully understand lifetimes when used in this context. I looked this up because I

Am I incorrectly implementing IntoIterator for a reference to a LazyList implementation or is this a Rust bug?

泪湿孤枕 提交于 2019-12-10 19:55:35
问题 In implementing a version of a LazyList (an immutable lazily-computed memoized singly-linked list, much as Haskell lists), I have run into a problem of implementing IntoIterator in that the code does not drop the reference when I think it should. The following code has been simplified so as just to show the problem; thus, is not generic and does not include all of the methods not related to implementing IntoIterator : use std::cell::UnsafeCell; use std::mem::replace; use std::rc::Rc; // only