静态生命周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//1.静态生命周期
//定义方法:'static
//生命周期存活于整个程序期间,所有的字符字面值都有static生命周期
//和C语言不同,C语言中static则是更换文件链接属性或者全局变量,Rust使用const
//let s: &'static str = "hello";

use std::fmt::Display;

fn function<'a,T:Display>(x: &'a str,y: &'a str,ann: T) -> &'a str {
println!("ann is {}",ann);
if x.len() < y.len()
{
x
}else {
y
}

}
fn main() {
let s1 = String::from("i am s1");
let s2 = String::from("i am s2");
let ann = 129;
let r = function(s1.as_str(),s2.as_str(),ann);
println!("r = {}",r);
}