David Brown
2014-08-13 05:45:36 UTC
I'm trying to figure out how to get something like this to compile.
What I want is a struct that owns a string as well as contains
structures containing slices of that string. My real use case is more
complicated, but it seems to boil down easily to this.
This gives me an error:
life.rs:16:21: 16:32 error: `result.text` does not live long enough
life.rs:16 result.fields = result.text.as_slice().split(' ').collect();
^~~~~~~~~~~
life.rs:13:40: 19:2 note: reference must be valid for the lifetime 'a as defined on the block at 13:39...
life.rs:13 fn build<'a>(text: String) -> Info<'a> {
life.rs:14 let mut result = Info { text: text, fields: Vec::new() };
life.rs:15
life.rs:16 result.fields = result.text.as_slice().split(' ').collect();
life.rs:17
life.rs:18 result
...
life.rs:13:40: 19:2 note: ...but borrowed value is only valid for the block at 13:39
life.rs:13 fn build<'a>(text: String) -> Info<'a> {
life.rs:14 let mut result = Info { text: text, fields: Vec::new() };
life.rs:15
life.rs:16 result.fields = result.text.as_slice().split(' ').collect();
life.rs:17
life.rs:18 result
...
Is there a way to do this, or do I need to allocate strings for the
copies of the individual slices?
Thanks,
David Brown
----------------------------------------------------------------------
// life.rs
fn main() {
let info = build(String::from_str("this is a test"));
println!("{}", info.fields[0]);
}
struct Info<'a> {
text: String,
fields: Vec<&'a str>,
}
fn build<'a>(text: String) -> Info<'a> {
let mut result = Info { text: text, fields: Vec::new() };
result.fields = result.text.as_slice().split(' ').collect();
result
}
----------------------------------------------------------------------
What I want is a struct that owns a string as well as contains
structures containing slices of that string. My real use case is more
complicated, but it seems to boil down easily to this.
This gives me an error:
life.rs:16:21: 16:32 error: `result.text` does not live long enough
life.rs:16 result.fields = result.text.as_slice().split(' ').collect();
^~~~~~~~~~~
life.rs:13:40: 19:2 note: reference must be valid for the lifetime 'a as defined on the block at 13:39...
life.rs:13 fn build<'a>(text: String) -> Info<'a> {
life.rs:14 let mut result = Info { text: text, fields: Vec::new() };
life.rs:15
life.rs:16 result.fields = result.text.as_slice().split(' ').collect();
life.rs:17
life.rs:18 result
...
life.rs:13:40: 19:2 note: ...but borrowed value is only valid for the block at 13:39
life.rs:13 fn build<'a>(text: String) -> Info<'a> {
life.rs:14 let mut result = Info { text: text, fields: Vec::new() };
life.rs:15
life.rs:16 result.fields = result.text.as_slice().split(' ').collect();
life.rs:17
life.rs:18 result
...
Is there a way to do this, or do I need to allocate strings for the
copies of the individual slices?
Thanks,
David Brown
----------------------------------------------------------------------
// life.rs
fn main() {
let info = build(String::from_str("this is a test"));
println!("{}", info.fields[0]);
}
struct Info<'a> {
text: String,
fields: Vec<&'a str>,
}
fn build<'a>(text: String) -> Info<'a> {
let mut result = Info { text: text, fields: Vec::new() };
result.fields = result.text.as_slice().split(' ').collect();
result
}
----------------------------------------------------------------------