I have defined two structs and a function in my Rust program, in which I am implementing my own linear algebra. While doing so I stumbled onto a gap in my knowledge of lifetimes in Rust. To my understanding, a lifetime annotation simply tells the compiler that one input variable must live at least as long as output variable.
But now I am actually trying to tell the compiler that my output is NOT related to the input. Sure, I take in references, but the new struct is constructed by creating new slices, and filling these with new values (f64).
So, my idea was that I have two inputs, whose lifetimes can be unrelated when the function is called, and a third one, saying it is unrelated to the inputs, because a new struct with no references to the old one is created. (See code below).
Sadly, this returns an error, and to be perfectly honest, I have no idea what it really means. I mean, I can read the words and know these words, but it doesn't really fit in the idea of lifetimes I have in my head. I need a fresh new look on lifetimes, or someone to point out to me what I understood wrong.
What I get from this is that it complains that my output is not related/constrained to the inputs, which is precisely what I want!
error[E0207]: the lifetime parameter `'c` is not constrained by the impl trait, self type, or predicates
--> src/mat.rs:199:14
|
199 | impl<'a, 'b, 'c
| ^^ unconstrained lifetime parameter
error[E0207]: the lifetime parameter `'c` is not constrained by the impl trait, self type, or predicates
I do not require any information on how to properly implement a matrix multiplication, or matrices, because I am toying around to learn Rust. I need an answer that shows me what I misunderstood about Rust.
The two structs:
pub struct ColVector<'a> {
values: &'a [f64],
}
pub struct Matrix<'a> {
values: &'a [&'a [f64]],
shape: (usize, usize) // Tuple containing size. First value is rows, second is columns
}
The implementation over-ride of the *-operator (I have 3 other ones, all giving the same error)
impl<'a, 'b, 'c> Mul<&'a ColVector<'a>> for &'b Matrix<'b> {
type Output = ColVector<'c>;
fn mul(self, v: &'a ColVector) -> ColVector<'c> {
self.mat_mult(v)
}
}
EDIT: I fail to see how this is a duplicate of the question Return local String as a slice (&str) . That question is about someone trying to return a reference to a variable local to the function, which is not what I am trying to do. My function does not return a reference, but a struct containing a reference to a newly created slice.