Skip to content

Instantly share code, notes, and snippets.

@kenpratt
Created June 23, 2014 18:55
Show Gist options
  • Save kenpratt/9152f97f47bb75611a40 to your computer and use it in GitHub Desktop.
Save kenpratt/9152f97f47bb75611a40 to your computer and use it in GitHub Desktop.
#[deriving(Show)]
enum Token<'a> {
Integer(int),
String(&'a str)
}
fn generate_int() -> Token {
Integer(23)
}
fn generate_str() -> Token {
let s = format!("just a test: {}", 42);
String(s.as_slice())
}
fn main() {
println!("{}", generate_int());
println!("{}", generate_str());
}
/*
string_as_enum_value.rs:13:12: 13:13 error: `s` does not live long enough
string_as_enum_value.rs:13 String(s.as_slice())
^
string_as_enum_value.rs:11:28: 14:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 11:27...
string_as_enum_value.rs:11 fn generate_str() -> Token {
string_as_enum_value.rs:12 let s = format!("just a test: {}", 42);
string_as_enum_value.rs:13 String(s.as_slice())
string_as_enum_value.rs:14 }
string_as_enum_value.rs:11:28: 14:2 note: ...but borrowed value is only valid for the block at 11:27
string_as_enum_value.rs:11 fn generate_str() -> Token {
string_as_enum_value.rs:12 let s = format!("just a test: {}", 42);
string_as_enum_value.rs:13 String(s.as_slice())
string_as_enum_value.rs:14 }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment