LINUX.ORG.RU

История изменений

Исправление red75prim, (текущая версия) :

Ну это и на расте можно


trait Push: Sized {
    fn push<T>(self, v: T) -> (Self, T) {
        (self, v)
    }
}

impl<T> Push for T {}

trait Pop {
    type T;
    type V;
    fn pop(self) -> (Self::T, Self::V);
}

impl<T, V> Pop for (T, V) {
    type T = T;
    type V = V;
    fn pop(self) -> (T, V) {
        self
    }
}


fn main() {
    let heterolist = 1.push("Hello").push("there".to_string()).push(());
    println!("{:?}", heterolist);
    let (rest, head) = heterolist.pop();
    println!("{:?}", rest);
}

Исходная версия red75prim, :

Ну это и на расте можно


trait Push: Sized {
    fn push<T>(self, v: T) -> (Self, T) {
        (self, v)
    }
}

impl<T> Push for T {}

trait Pop {
    type T;
    type V;
    fn pop(self) -> (Self::T, Self::V);
}

impl<T, V> Pop for (T, V) {
    type T = T;
    type V = V;
    fn pop(self) -> (T, V) {
        (self.0, self.1)
    }
}


fn main() {
    let heterolist = 1.push("Hello").push("there".to_string()).push(());
    println!("{:?}", heterolist);
}