【发布时间】:2015-09-22 16:46:31
【问题描述】:
我阅读了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
不过加个参数就OK了:
fn get_str(s: &str) -> &str {
"Hello World"
}
为什么会这样? "Hello World"如何借用参数s,即使它与s无关?
【问题讨论】:
标签: string rust lifetime string-literals