rust-unofficial / too-many-lists

Learn Rust by writing Entirely Too Many linked lists
https://rust-unofficial.github.io/too-many-lists/
MIT License
3.16k stars 276 forks source link

Fourth.rs note, and printing. #262

Open X01XX opened 1 year ago

X01XX commented 1 year ago

In fourth.rs, my cargo, (edition = "2018"), wants Node prev and next Boxed. It was not too hard to make it work. How about printing? Something like:

impl<T: std::fmt::Display> fmt::Display for List<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut rc_str = String::from("(");

        let mut cur_link = &self.head;
        let mut start = true;

        while let Some(boxed_node) = cur_link {
            if start {
            } else {
                rc_str.push_str(", ");
            }
            start = false;
            rc_str.push_str(&format!("{}", boxed_node.elem));
            cur_link = &boxed_node.next;
        }
        rc_str.push_str(")");

        write!(f, "{}", rc_str)
    }
}
X01XX commented 1 year ago

Improved code formatting.