简体   繁体   English

如何修复丢失的生命周期说明符?

[英]How do I fix a missing lifetime specifier?

I have a very simple method. 我有一个非常简单的方法。 The first argument takes in vector components ("A", 5, 0) and I will compare this to every element of another vector to see if they have the same ( _ , 5 , _) and then print out the found element's string. 第一个参数采用向量组件(“A”,5,0),我将它与另一个向量的每个元素进行比较,看它们是否具有相同的(_,5,_),然后打印出找到的元素的字符串。

Comparing ("A", 5, 0 ) and ("Q", 5, 2) should print out Q. 比较(“A”,5,0)和(“Q”,5,2)应打印出Q.

fn is_same_space(x: &str, y1: i32, p: i32, vector: &Vec<(&str, i32, i32)>) -> (&str) {
    let mut foundString = "";

    for i in 0..vector.len() {

        if y1 == vector[i].1 {
            foundString = vector[i].0;
        }

    }
    foundString    
}

However, I get this error 但是,我收到此错误

error[E0106]: missing lifetime specifier
 --> src/main.rs:1:80
  |
1 | fn is_same_space(x: &str, y1: i32, p: i32, vector: &Vec<(&str, i32, i32)>) -> (&str) {
  |                                                                                ^ expected 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 one of `vector`'s 2 elided lifetimes

By specifying a lifetime : 通过指定生命周期

fn is_same_space<'a>(x: &'a str, y1: i32, p: i32, vector: &'a Vec<(&'a str, i32, i32)>) -> (&'a str)

This is only one of many possible interpretations of what you might have meant for the function to do, and as such it's a very conservative choice - it uses a unified lifetime of all the referenced parameters. 这只是对函数执行操作的许多可能解释中的一种,因此它是一种非常保守的选择 - 它使用所有引用参数的统一生命周期。

Perhaps you wanted to return a string that lives as long as x or as long as vector or as long as the strings inside vector ; 也许你想要返回一个字符串,它的长度与x一样长,或者与vector一样长,或者只要字符串在vector ; all of those are potentially valid. 所有这些都可能有效。


I strongly recommend that you go back and re-read The Rust Programming Language . 强烈建议您返回并重新阅读The Rust Programming Language It's free, and aimed at beginners to Rust, and it covers all the things that make Rust unique and are new to programmers. 它是免费的,针对Rust的初学者,它涵盖了使Rust独特的所有东西,对程序员来说是新手。 Many people have spent a lot of time on this book and it answers many beginner questions such as this one. 许多人花了很多时间在这本书上,它回答了很多初学者的问题,比如这个问题。

Specifically, you should read the chapters on: 具体来说,您应该阅读以下章节:

There's even a second edition in the works , with chapters like: 甚至还有第二版的作品 ,章节如下:


For fun, I'd rewrite your code using iterators: 为了好玩,我会使用迭代器重写你的代码:

fn is_same_space<'a>(y1: i32, vector: &[(&'a str, i32, i32)]) -> &'a str {
    vector.iter()
        .rev() // start from the end
        .filter(|item| item.1 == y1) // element that matches
        .map(|item| item.0) // first element of the tuple
        .next() // take the first (from the end)
        .unwrap_or("") // Use a default value
}

So the problem comes from the fact that vector has two inferred lifetimes, one for vector itself (the &Vec part) and one for the &str inside the vector. 所以问题来自于vector有两个推断的生命周期,一个用于vector本身( &Vec部分),另一个用于向量内的&str You also have an inferred lifetime on x , but that really inconsequential. 你在x上也有一个推断的生命周期,但这确实无关紧要。

To fix it, just specify that the returned &str lives as long as the &str in the vector: 为了解决这个问题,只需指定,返回&str只要住的&str的载体:

fn is_same_space<'a>(                        // Must declare the lifetime here
    x: &str,                                 // This borrow doesn't have to be related (x isn't even used)
    y1: i32,                                 // Not borrowed
    p: i32,                                  // Not borrowed or used
    vector: &'a Vec<(&'a str, i32, i32)>     // Vector and some of its data are borrowed here
) -> &'a str {                               // This tells rustc how long the return value should live
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实现结构特征时,为什么会出现“缺少生存期说明符”或“类型参数数量错误”的情况? - Why do I get “missing lifetime specifier” or “wrong number of type arguments” when implementing a trait for a struct? 如何解决这个终身问题? - How do I fix this lifetime issue? 获取“缺少生命周期说明符”错误 - Getting 'Missing Lifetime specifier' error 返回的 HashMap&lt;&amp;i32, SimpleCallback&gt; 中缺少生命周期说明符 - Missing lifetime specifier in returned HashMap<&i32, SimpleCallback> 为什么我在这里需要'静态生命周期以及如何解决它? - Why do I need 'static lifetime here and how to fix it? 如何在Rust的装饰器模式中使用生命周期说明符? - How to use lifetime specifier in decorator pattern in Rust? 在结构中存储 &str 时,“缺少生命周期说明符”是什么意思? - What does "missing lifetime specifier" mean when storing a &str in a structure? 函数签名上缺少生命周期说明符[E0106] - missing lifetime specifier [E0106] on function signature 缺少类型别名上的生命周期说明符 [E0106] - missing lifetime specifier [E0106] on type alias 从 trait 方法返回对结构字段的可变引用时,如何修复生命周期不匹配? - How do I fix the lifetime mismatch when returning a mutable reference to the struct's field from a trait method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM