rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.34k stars 12.72k forks source link

Struct field lifetime error and misleading error message. #20491

Closed nulldatamap closed 9 years ago

nulldatamap commented 9 years ago

The following code results in a misleading error message and is not intended behaviour.

pub struct Wowser<'a> {
  wow : &'a mut str,
}

impl<'a> Wowser<'a> {
  pub fn new( wowling : &'a mut str ) -> Wowser<'a> {
    Wowser{ wow: wowling }
  }
}

Error:

error: mismatched types: expected `&mut str`, found `&'a mut str` (values differ in mutability)
     Wowser{ wow: wowling }
                  ^~~~~~~

Passing a new lifetime to the function will result in the same error:

pub fn new<'b>( wowling : &'b mut str ) -> Wowser<'b> {
steveklabnik commented 9 years ago

This example now compiles.