Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fourth.rs note, and printing. #262

Open
X01XX opened this issue Dec 19, 2022 · 1 comment
Open

Fourth.rs note, and printing. #262

X01XX opened this issue Dec 19, 2022 · 1 comment

Comments

@X01XX
Copy link

X01XX commented Dec 19, 2022

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
Copy link
Author

X01XX commented Dec 20, 2022

Improved code formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant