t_wの輪郭

Feedlyでフォローするボタン

missing lifetime specifier

2022/2/20 23:34:00
fn longest(x: &str, y: &str) -> &str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

fn main() {
    let str1 = "str1";
    let str2 = "str2 hogehoge";
    let long = longest(&x, &y);
}
error[E0106]: missing lifetime specifier
 --> src\main.rs:1:33
  |
1 | fn longest(x: &str, y: &str) -> &str {
  |               ----     ----     ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y`
help: consider introducing a named lifetime parameter
  |
1 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
  |           ++++     ++          ++          ++

あれ

2022/2/20 23:45:00
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

fn main() {
    let str1 = "str";
    let str2 = "long str";
    let long_str = longest(str1, str2);
    println!("long_str is {}", long_str);   //long_str is long str
}

error[E0106]: missing lifetime specifier
–> src\lib.rs:5:51
|
5 | pub fn search(query: &str, contents: &str) -> Vec<&str> {
| ── ── ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from query or contents
help: consider introducing a named lifetime parameter
|
5 | pub fn search<'a>(query: &'a str, contents: &'a str) -> Vec<&'a str> {
| ++++ ++ ++ ++

For more information about this error, try rustc --explain E0106.