我读了tutorial on the official website,对常量字符串/字符串文本的生存期有一些疑问.
我在编写以下代码时出错:
fn get_str() -> &str {
"Hello World"
}
错误:
error[E0106]: missing lifetime specifier
--> src/main.rs:1:17
|
1 | fn get_str() -> &str {
| ^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
= help: consider giving it a 'static lifetime
但是,当我添加一个参数时,它是可以的:
fn get_str(s: &str) -> &str {
"Hello World"
}
为什么会这样?"Hello World"
如何从参数s
borrow ,即使它与s
无关?