Skip to content

ライフタイムの解説のサンプルコード実行時に発生するエラーメッセージが説明と異なる #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
takinaga-dev opened this issue May 12, 2020 · 1 comment · Fixed by #93
Milestone

Comments

@takinaga-dev
Copy link

ライフタイムの観点で思考するで記載されているサンプルコード実行時に発生するエラーメッセージが解説されているものと異なります。

環境

% rustc -V; cargo --version
rustc 1.41.0 (5e1a79984 2020-01-27)
cargo 1.41.0 (626f0f40e 2019-12-03)

説明のコードとエラーメッセージ

// コード
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

@tatsuya6502
Copy link
Member

対応が遅く、申し訳ありません。本件は #86 という、ライフタイムの章(10-03)の内容を英語版の最新のものと同じにするissueの中で対応しようと思います。

@tatsuya6502 tatsuya6502 added this to the 2018 Ed. 02 milestone Sep 13, 2020
@tatsuya6502 tatsuya6502 removed their assignment Sep 13, 2020
@swnakamura swnakamura mentioned this issue Sep 24, 2020
3 tasks
@tatsuya6502 tatsuya6502 linked a pull request Sep 24, 2020 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants