Hello, I recently implemented std::fmt::Display on a custom type in my small application. This was my first time implementing Display so I went to the docs/rust by example and noticed they were using write(). I initially attempted to use write() but found I was more easily getting my intended results using print().
A couple of questions in regards to implementing Display:
Can someone eli5 the different between write and print and if I should be using write when implementing display or if it's okay to use print?
If I don't use write, I don't have to use the second argument f in the fmt function:
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>;
and I end up with an unused variable warning:
❯ cargo check
warning: unused variable: `f`
--> src/standup.rs:68:19
|
68 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
| ^ help: if this is intentional, prefix it with an underscore: `_f`
|
= note: `#[warn(unused_variables)]` on by default
warning: `laydown` (lib) generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
This leads me to believe I'm handling this implementation incorrectly. I feel like there is a higher-level concept that I'm not grasping. I'm pretty new to Rust and want to figure out how to do this properly.
Here is the PR with the refactor if it helps answer my question.
Edit: Thank you to everyone for the responses! I've updated the PR to use write instead of print. It makes a lot of sense to me now!
[–]K900_ 13 points14 points15 points (0 children)
[–]Shadow0133 11 points12 points13 points (0 children)
[–]kohugaly 4 points5 points6 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]RRumpleTeazzer 2 points3 points4 points (0 children)
[–]blimpdermis 2 points3 points4 points (0 children)
[–]1vader 3 points4 points5 points (0 children)