[英]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
}
camelCase
variable names. camelCase
变量名。 vector
. vector
内部返回字符串。 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.