There are a bunch of methods we call in Rust all the time that do a small, common thing, with too much visual noise. Think clone and to_owned. Etcetera.
For quite a few months now I’ve been using some convenience functions to make a few common Rust operations less visually cluttered, and I’m finding I like it - every time I see an unwrap I wish I had my … POWERLETTERS!
Haha.
It’s super simple, as described in the crate docs. The powerletters are single all-caps functions and methods:
- C - Clone
- O - ToOwned
- S - ToString
- I - Ignore Result
- X - expect for Result and Option
In action:
Pretty obvious stuff. The only non-obvious powerletter is I. It doesn’t correspond to a trait or method. It instead makes the let _ = some_result pattern typesafe. And less of a syntactic odball.
Consider the example:
What is actually going on here? Probably ignoring a Result? What is the return type of do_something_important? It doesn’t matter; we’re ignoring it.
What if we changed do_something_important from
to
Now we’re just silently dropping a future and it looks intentional. It’s happened to me!
How about this:
Anyway enjoy some powerletters in action: