-
-
Save aisamanra/615772f6a26bfb438581b36d093ee064 to your computer and use it in GitHub Desktop.
a junction but in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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