这段代码可以编译并且运行,但是根据我的理解它不应该能够编译:
use std::fmt::Display;
pub fn test<S>(s: S)
where
S: Display + 'static,
{
println!("test: {}", s);
}
fn main() {
let s = String::from("string");
test(s);
}
< p > 变量s
的生命周期在main
中,但函数test
有一个绑定,要求S
必须是'static'
。我认为变量s
的生命周期必须是'static'
或大于'static'
。我的推理有什么问题吗?< /p >