12345678910111213141516171819202122232425262728293031323334struct StuA<'a> { name: &'a str, }impl<'a> StuA<'a> { fn do_something(&self) -> i32 { //输出不是引用,所以不用写 3 } fn do_something2(&self,s: &str) -> &str { //省略规则3 //fn do_something2<'b>(&'b self,s: &str) -> &'b str { self.name } //报错 //需要显示标明// fn do_something3(&self,s: &str) -> &str {//没法使用,因为s和输出生命周期不同// s// } //---------------------------------- //显示标明之后 fn do_something3<'a>(&self,s: &'a str) -> &'a str { s } }fn main() { let s = String::from("Hello"); let a = StuA(name: &s); let s2 = String::from("hello"); println!("{}",a.do_something()); println!("{}",a.do_something2(&s2)); println!("{}",a.do_something3(&s2)); }