Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 16, 2021 13:38
Show Gist options
  • Save rust-play/88f2fbd8464ede697605101f29dbcda0 to your computer and use it in GitHub Desktop.
Save rust-play/88f2fbd8464ede697605101f29dbcda0 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::ops::Sub;
#[derive(Debug, Clone)]
struct Vector<T, const N: usize>([T; N]);
impl<'a, 'b, T, const N: usize> Sub<&'a Vector<T, N>> for &'b Vector<T, N>
where
&'b T: Sub<&'a T, Output = T>,
{
type Output = Vector<T, N>;
fn sub(self: &'b Vector<T, N>, other: &'a Vector<T, N>) -> Self::Output {
unimplemented!()
}
}
fn test<T, const N: usize>(p: &Vector<T, N>) -> Vector<T, N>
where
for<'a> &'a T: Sub<&'a T, Output = T>,
{
p - p
}
fn main() {
let a: Vector<i32, 2> = Vector([1, 1]);
let b = Vector([2, 2]);
println!("Output: {:?}", &a - &b); // This works
println!("Output: {:?}", test(&a)); // This fails
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment