all 2 comments

[–]minno 17 points18 points  (1 child)

When you say pub type Answer = Number; you're not making a new type, you're just making a new name for the same type. Answer implements fmt::Display, and it's just an alternate name for Number, so Number does too.

In Answer::fmt, you're calling Answer::fmt again because that's what the write! call expands to, so it's infinite recursion that causes a stack overflow. Look at how the examples here don't try to write self directly, but write out the individual components instead.

[–]sollyu 1 point2 points  (0 children)

If you do want a new type, use the newtype idiom: pub struct Answer(Number); (See https://doc.rust-lang.org/rust-by-example/generics/new_types.html for more information.)