I'm trying to comprehend what would be the correct implementation for the following code:
use std::ops::Sub;
use std::cmp::min;
struct Pair<Value> {
a: Vec<Value>,
b: Vec<Value>,
c: Vec<Value>,
}
impl<'a, Value> Pair<Value>
where
Self: 'a,
Value: Ord + Clone + Sub<&'a Value, Output = Value>,
{
fn min(&self, idx: usize) -> &Value {
min(&self.a[idx], &self.b[idx])
}
fn gap(&'a self, idx: usize) -> Value {
self.b[idx].clone() - self.min(idx)
}
}
I just have some inner feeling that I'm misusing Rust here ))
, because for i32
code looks like here:
impl Pair<i32> {
fn min(&self, idx: usize) -> &i32 {
min(&self.a[idx], &self.b[idx])
}
fn gap(&self, idx: usize) -> i32 {
self.b[idx] - self.min(idx)
}
}
I mostly care about gap
function, lifetimes and cloning as that would be the part of code related to intensive computations. In practice Value
can be represented by complex structures, so Copy
trait is not an option here.