【发布时间】:2017-09-05 22:29:09
【问题描述】:
我有一个非常简单的方法。第一个参数接受向量分量(“A”、5、0),我将把它与另一个向量的每个元素进行比较,看看它们是否相同(_、5、_),然后打印出找到的元素的字符串。
比较 ("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
}
但是,我得到了这个错误
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
【问题讨论】:
-
这里的重点是您返回的
&str属于某人。编译器想要知道某人是谁,因此它可以知道您&str指向的内存预计会存在多长时间。您需要告诉编译器“只要传入的引用,我返回的&str就会存在”