struct StuA<'a> {
name: &'a str,
}
impl<'b> StuA<'b> {
fn do_something(&self) -> i32 {
3
}
fn do_something2(&self, s: &str) -> &str {
// fn do_something2<'b>(&'b self, s: &str) -> &'b str{
self.name
}
fn do_something3<'a>(&self, s: &'a str) -> &'a str {
s
}
}
fn main() {
let s = String::from("hello");
let a = StuA { name: &s };
println!("{}", a.do_something());
let s2 = String::from("hello");
println!("{}", a.do_something2(&s2));
println!("{}", a.do_something3(&s2));
println!("Hello, world!");
}
本节全部源代码:
https://github.com/anonymousGiga/learn_rust/blob/master/learn_life4/src/main.rs
来源:oschina
链接:https://my.oschina.net/u/943779/blog/4880848