54674

Why do these both structs differ in the way they are affected by lifetimes

Question:

This is a follow up question to: <a href="https://stackoverflow.com/questions/24291288/how-to-fix-cannot-infer-an-appropriate-lifetime-for-automatic-coercion" rel="nofollow">How to fix: cannot infer an appropriate lifetime for automatic coercion</a>.

I wonder why do these both structs differ in the way they are affected by lifetimes.

<strong>Example 1</strong>

use http; pub struct Request<'a> { pub origin: &'a http::server::Request, }

<strong>Example 2</strong>

use http; pub struct Response<'a, 'b> { pub origin: &'a mut http::server::ResponseWriter<'b>, }

They look pretty much similar to me except that the second one holds a mutable reference whereas the first one holds an immutable reference.

However, for <em>Example 2</em> I can't just use a lifetime for the reference. I <strong>must</strong> give a lifetime for the struct as well.

So, I wonder is there something <em>inside</em> the struct that causes such behavior or is it really because the one in the second example is a mutable reference. And if so, why exactly does that cause that.

Answer1:

&'a T means that you have a reference to a T object which is valid for the lifetime'a.

T<'b> means a T object containing an object inside itself valid for the lifetime 'b, as in struct T<'b> { t: &'b U }.

&'a T<'b>is thus a reference with lifetime 'a to a T<'b> object.

In the case of the ResponseWriter, it contains references to the Request and to the TcpStream, whereas the Request does not contain any references.

Recommend

  • calling -retain multiple times on the same object
  • How can multiple struct fields be generics that use the same higher-kinded lifetime?
  • Why do I get a lifetime error when I use a mutable reference in a struct instead of an immutable ref
  • Can I do type introspection with trait objects and then downcast it?
  • Does setting autocommit=0 within a transaction do anything?
  • wait function that uses while
  • Migration of Google Project to Firebase Console
  • What drive the natural result order for an unordered MySQL request
  • Why does the address of an object change across methods?
  • is this code truly private? (python)
  • Accessing Windows Azure Queues from client side javascript/jquery
  • Complex trait requirements on struct
  • JSON - slashes not escaping
  • Why doesn't a local variable live long enough for thread::scoped?
  • Google Maps api v3 get start and end coordinates of a street
  • Monotouch crashes with NullReferenceException on non nullable object
  • sweetalert2 inputoptions from file in select example
  • Simple linked list-C
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • Bug in WPF DataGrid
  • TFS: Get latest causes slow project reloading
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Javascript Callbacks with Object constructor
  • Symfony2: How to get request parameter
  • How to make Safari send if-modified-since header?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • log4net write single file for each call to log.info
  • Getting error when using KSoap library to consume .NET web services
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • git trying to push non-existent file … after clearing cache
  • java string with new operator and a literal