Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(box_syntax)]
- trait Blorpable: std::fmt::Debug {
- fn blorp(&self, n: i32) -> i32;
- }
- #[derive(Debug, Clone)]
- struct BlorpAdd(i32);
- impl Blorpable for BlorpAdd {
- fn blorp(&self, n: i32) -> i32 {
- self.0 + n
- }
- }
- #[derive(Debug, Clone)]
- struct BlorpSub(i32);
- impl Blorpable for BlorpSub {
- fn blorp(&self, n: i32) -> i32 {
- self.0 - n
- }
- }
- #[derive(Debug, Clone)]
- struct Blorper<T: Blorpable + Clone>(Vec<Box<T>>);
- impl<T: Blorpable + Clone> Blorper<T> {
- fn blorp_all(&self, n: i32) -> i32 {
- self.0.iter().fold(n, |acc, blp| blp.blorp(acc))
- }
- }
- impl<T: Blorpable + Clone> Blorpable for Blorper<T> {
- fn blorp(&self, n: i32) -> i32 {
- self.blorp_all(n)
- }
- }
- fn main() {
- let blorper = Blorper(vec![
- box BlorpAdd(2),
- box Blorper(vec![
- box BlorpAdd(6),
- box BlorpSub(2),
- ]),
- box BlorpSub(3),
- ]);
- println!("{}", blorper.blorp(10))
- }
Add Comment
Please, Sign In to add comment