如何将函数参数中的项目推送到函数参数中的向量 - 不同的生命周期问题

How to push a item from a function parameter to a vector from a function parameter - Different lifetime issue

我需要将项目推送到矢量。但是item和vector都是函数参数

重现的简单代码:-

struct Point {
    x: i32,
    y: i32
}

fn main(){

    let points: Vec<&Point> = vec!();
    let start_point = Point {x: 1, y:2};

    fn fill_points<F>(points: &mut Vec<&F>, point: &F ){
        // Some conditions and loops
        points.push(point);
    }

    fill_points(&mut points, & start_point);
}

错误:-

   |
13 |     fn fill_points<F>(points: &mut Vec<&F>, point: &F ){
   |                                        --          -- these two types are declared with different lifetimes...
14 |         // Some conditions
15 |         points.push(point);
   |                     ^^^^^ ...but data from `point` flows into `points` here

游乐场URL:- https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e1f2d738a22795ae11aab01ad8d00d96

您需要添加生命周期参数:

struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let start_point = Point { x: 1, y: 2 };
    let mut points: Vec<&Point> = vec![];

    fn fill_points<'a, F>(points: &mut Vec<&'a F>, point: &'a F) {
        // Some conditions
        points.push(point);
    }

    fill_points(&mut points, &start_point);
}

(基于来自@Stargateur 的 comment 简化的代码)