Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@MrSpock
Last active April 12, 2017 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrSpock/d227cf06618b301f33936e6f55b6696b to your computer and use it in GitHub Desktop.
Save MrSpock/d227cf06618b301f33936e6f55b6696b to your computer and use it in GitHub Desktop.
lifetime-fight-001
//=========== attempt 1 =============
trait Handler<'a> {
fn handle(self, String) -> &'a str;
}
#[derive(Debug)]
struct TestHandle {
uri: String,
}
impl<'a> Handler<'a> for TestHandle {
fn handle(self, s: String) -> &'a str {
format!("via {}", s)
}
}
struct A<S>
where S: Handler
{
name: String,
handler: Box<S>,
}
//error[E0106]: missing lifetime specifier
// --> src/main.rs:18:14
// |
//18 | where S: Handler
// | ^^^^^^^ expected lifetime parameter
//
//error: aborting due to previous error
//===== attempt 2 =============
struct A<S>
where S: Handler<'a>
{
name: String,
handler: Box<S>,
}
//Error:
//error[E0261]: use of undeclared lifetime name `'a`
// --> src/main.rs:18:22
// |
//18 | where S: Handler<'a>
// | ^^ undeclared lifetime
error: aborting due to previous error
// attempt 3 ===============
struct A<'a, S>
where S: Handler<'a>
{
name: String,
handler: Box<S>,
}
//error[E0392]: parameter `'a` is never used
// --> src/main.rs:17:10
// |
//17 | struct A<'a, S>
// | ^^ unused type parameter
// |
// = help: consider removing `'a` or using a marker such as `std::marker::PhantomData`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment