Skip to content

Instantly share code, notes, and snippets.

@kenpratt
Created June 23, 2014 19:06
Show Gist options
  • Save kenpratt/7ed7809f8d8af9363053 to your computer and use it in GitHub Desktop.
Save kenpratt/7ed7809f8d8af9363053 to your computer and use it in GitHub Desktop.
#[deriving(Show)]
struct SampleError<'a> {
message: &'a str
}
fn generate_err() -> Result<(), SampleError> {
let m = format!("sample dynamic error: {}", 42);
Err(SampleError { message: m.as_slice() })
}
fn main() {
println!("{}", generate_err());
}
/*
string_in_error.rs:8:32: 8:33 error: `m` does not live long enough
string_in_error.rs:8 Err(SampleError { message: m.as_slice() })
^
string_in_error.rs:6:46: 9:2 note: reference must be valid for the anonymous lifetime #1 defined on the block at 6:45...
string_in_error.rs:6 fn generate_err() -> Result<(), SampleError> {
string_in_error.rs:7 let m = format!("sample dynamic error: {}", 42);
string_in_error.rs:8 Err(SampleError { message: m.as_slice() })
string_in_error.rs:9 }
string_in_error.rs:6:46: 9:2 note: ...but borrowed value is only valid for the block at 6:45
string_in_error.rs:6 fn generate_err() -> Result<(), SampleError> {
string_in_error.rs:7 let m = format!("sample dynamic error: {}", 42);
string_in_error.rs:8 Err(SampleError { message: m.as_slice() })
string_in_error.rs:9 }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment