Instead of implementing `ToString` directly, you should implement `Display`. A `ToString` implementation will automatically be added, and you get all the formatting goodness out of the box.
Re:Show HN: Risp – Lisp in Rust
by thenewwazoo on Thursday May 02, 2019 @04:34PM
(#19812588)
That's a good idea! I don't often impl Display so it didn't even occur to me.
As a Lisp programmer, the way most other languages handle errors make me cringe. How could I even make a decent ice cream sundae without error recovery? But I do enjoy that 'make a Lisp' is a like a sport in every other language.
http://www.lispworks.com/documentation/HyperSpec/Body/f_cerr...
http://www.lispworks.com/documentation/HyperSpec/Body/m_rst_...
Oof, I love this — thank you thenewwazoo!
I gotta get to sleep, but will either update the post or add a note in the morning.
And of course, will do this in all future rust projects :D
Re:Show HN: Risp – Lisp in Rust
by thenewwazoo on Thursday May 02, 2019 @04:09PM
(#19812247)
Glad to help! I am a huge huge fan of Rust. I'm almost always hanging out on ##rust and ##rust-beginners on Freenode if you want to ask questions synchronously. :)
Use the "?" operator. That way, you only handle errors one time (probably at the root of your program).
See here: https://github.com/omarabid/rust-starter/blob/master/src/mai...
For example, if your errors are:
You can impl ToString straight away: Then, when handling the error, you can where do_thing's signature looks like It takes the same amount of code, but all your error messages are in the same place, you haven't lost information about them, and refactoring to handle them becomes super easy.(n.b. I dashed this comment off without actually compiling the above, so please forgive any dumb errors =] )
[1] https://doc.rust-lang.org/std/string/trait.ToString.html