我有一個非常簡單的方法。第一個參數需要向量組件(「A」,5,0),我會將它與另一個向量的每個元素進行比較,以查看它們是否具有相同的(_,5,_),然後打印出找到的元素的字符串。如何修復缺少的生命週期說明符?
比較( 「A」,5,0)和( 「Q」,5,2)應該打印出來問:
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'指向的內存預期會存活多久。你需要告訴編譯器:「只要在」 – turbulencetoo