You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// コード
fn longest<'a>(x: &str, y: &str) -> &'a str {
// 本当に長い文字列
let result = String::from("really long string");
result.as_str()
}
//エラーメッセージ
error[E0597]: `result` does not live long enough
--> src/main.rs:3:5
|
3 | result.as_str()
| ^^^^^^ does not live long enough
4 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifeti
実際に発生するエラーメッセージ
error[E0515]: cannot return value referencing local variable `result`
--> src/main.rs:66:3
|
66 | result.as_str()
| ------^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `result` is borrowed here
ライフタイムの観点で思考するで記載されているサンプルコード実行時に発生するエラーメッセージが解説されているものと異なります。
環境
説明のコードとエラーメッセージ
実際に発生するエラーメッセージ
参考
https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#thinking-in-terms-of-lifetimes
The text was updated successfully, but these errors were encountered: