I'm trying to turn a matrix A into matrix B where B_11 would be the biggest element of the first row and the smallest element of the second row in a tuple. So the matrix [1,2;3,4] would become [(2,1), (2,2); (4,1), (4,2)].
pub fn find_saddle_points(input: &[Vec<u64>]) -> Vec<(usize, usize)> {
let answer = Vec::new();
println!("matrix: {:?}", input);
let x: Vec<Vec<_>> = input
.iter()
.map(|row| {
row.iter()
.map(move |_| {
let y = get_column(&input, 0);
println!("y is {:?}", y);
let x = get_lowest(&y);
(get_highest(row), x)
}).collect()
}).collect();
println!("x is {:?}", x);
answer
}
fn get_highest(row: &Vec<u64>) -> &u64 {
row.iter().max().unwrap()
}
fn get_lowest(column: &Vec<u64>) -> &u64 {
column.iter().min().unwrap()
}
fn get_column(matrix: &[Vec<u64>], index: usize) -> Vec<u64> {
matrix.iter().map(|row| row[index]).collect()
}
I'm getting an error messages when I try to use y
:
error[E0597]: `y` does not live long enough
--> src/lib.rs:11:41
|
11 | let x = get_lowest(&y);
| ^ borrowed value does not live long enough
12 | (get_highest(row), x)
13 | }).collect()
| - `y` dropped here while still borrowed
...
17 | }
| - borrowed value needs to live until here
I don't understand why the value y
has to live until after it's used. Isn't it enough that it lives until after the get_lowest
function?
How would I fix this?