t_wの輪郭

Feedlyでフォローするボタン
Rust生存期間せいぞんきかんしていし指定子lifetime specifierライフタイム指定子
&'Rustでは関数で作成した変数への参照を戻り値で渡すことはできない

&'

2022/2/20 23:16:00
fn main() {
    let reference_to_nothing = dangle();
}

fn dangle() -> &String {
    let s = String::from("hello");

    &s
}
error[E0106]: missing lifetime specifier
 --> src\main.rs:5:16
  |
5 | fn dangle() -> &String {
  |                ^ expected named 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 using the `'static` lifetime
  |
5 | fn dangle() -> &'static String {
  |                ~~~~~~~~