Skip to content

Instantly share code, notes, and snippets.

@aisamanra
Created January 26, 2018 01:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aisamanra/615772f6a26bfb438581b36d093ee064 to your computer and use it in GitHub Desktop.
Save aisamanra/615772f6a26bfb438581b36d093ee064 to your computer and use it in GitHub Desktop.
a junction but in rust
use std::cmp::{PartialEq};
use std::ops::{Add};
#[derive(Clone)]
struct Junction<T> {
values: Vec<T>,
}
impl<T: Clone> Junction<T> {
fn new(values: &[T]) -> Self {
Junction { values: values.to_vec() }
}
}
impl<T: Add<Output=T> + Clone> Add<T> for Junction<T> {
type Output = Junction<T>;
fn add(self, other: T) -> Junction<T> {
let mut new_vec = Vec::new();
for v in self.values {
new_vec.push(v + other.clone());
}
Junction {
values: new_vec,
}
}
}
impl<T: PartialEq<T>> PartialEq<T> for Junction<T> {
fn eq(&self, other: &T) -> bool {
for v in self.values.iter() {
if v.eq(other) {
return true;
}
}
false
}
}
fn main() {
let v1 = Junction::new(&[2, 3, 4]);
let v2 = v1 + 1;
if v2 == 5 {
println!("yes");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment