Editor's note: The syntax in this question predates Rust 1.0 and the 1.0-updated syntax generates different errors, but the overall concepts are still the same in Rust 1.0.
I have a struct T with a name
field, I'd like to return that string from the name
function. I don't want to copy the whole string, just the pointer:
struct T {
name: ~str,
}
impl Node for T {
fn name(&self) -> &str { self.name }
// this doesn't work either (lifetime error)
// fn name(&self) -> &str { let s: &str = self.name; s }
}
trait Node {
fn name(&self) -> &str;
}
Why is this wrong? What is the best way to return an &str
from a function?