如何修复缺少的生命周期说明符? [英] How do I fix a missing lifetime specifier?

查看:74
本文介绍了如何修复缺少的生命周期说明符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的方法.第一个参数接受矢量分量("A",5,0),然后将其与另一个矢量的每个元素进行比较,以查看它们是否具有相同的分量(_,5,_),然后打印出找到的元素的字符串.

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)和("Q",5,2),应打印出Q.

Comparing ("A", 5, 0 ) and ("Q", 5, 2) should print out 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    
}

但是,我收到此错误

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

推荐答案

通过指定生存期:

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.

也许您想返回一个字符串,该字符串的生存期与xvector一样,或与vector中的字符串一样长;所有这些都可能有效.

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; all of those are potentially valid.

强烈建议,您回去重新阅读 Rust编程语言 .它是免费的,面向Rust的初学者,它涵盖了使Rust独特且对程序员来说是新事物的所有方面.许多人在这本书上花费了很多时间,并且回答了很多初学者的问题,例如这本书.

I strongly recommend that you go back and re-read 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. 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:

  • ownership
  • references and borrowing
  • lifetimes

甚至还有第二版,像这样的章节:

There's even a second edition in the works, with chapters like:

  • Understanding Ownership
  • Generic Types, Traits, and Lifetimes

为了娱乐,我将使用迭代器重写您的代码:

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
}

  • 删除了不需要的参数.
  • 使用迭代器避免了边界检查的开销,并且更清楚地揭示了您的意图.
  • 为什么不鼓励接受对String(& String)或Vec(& Vec)的引用作为函数参数?
  • Rust不使用camelCase变量名.
  • 我假设您确实想从vector内部返回字符串.
  • 删除返回类型上的多余括号
    • Removed the unneeded parameters.
    • Using an iterator avoids the overhead of bounds checks, and more clearly exposes your intent.
    • Why is it discouraged to accept a reference to a String (&String) or Vec (&Vec) as a function argument?
    • Rust does not use camelCase variable names.
    • I assume that you do want to return the string from inside vector.
    • Remove the redundant parens on the return type
    • 这篇关于如何修复缺少的生命周期说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆