Skip to content

std::fmt needs documentation on how to implement the traits #9865

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

Closed
chris-morgan opened this issue Oct 15, 2013 · 0 comments · Fixed by #9938
Closed

std::fmt needs documentation on how to implement the traits #9865

chris-morgan opened this issue Oct 15, 2013 · 0 comments · Fixed by #9938

Comments

@chris-morgan
Copy link
Member

OK, so we have our fancy new formatting with its traits and all, but at present it's not obvious as (most commonly) a library developer how to implement its goodness for your own types.

We should have a new section in the std::fmt documentation on "how to implement these traits". I think the general advice should be to use write!. It could have an example like this:

use std::fmt;

struct Vector2D {
    x: int,
    y: int,
}

impl fmt::Default for Vector2D {
    fn fmt(obj: &Vector2D, f: &mut fmt::Formatter) {
        write!(f.buf, "({}, {})", obj.x, obj.y)
    }
}

(I still hate the way it's a static method with an explicit first argument rather than a regular method with &self. But ah! there is no syntax to express the other at present.)

This is a very basic example; more will be needed for the adventurous, covering flags, padding and so forth, how f.buf is a Writer which means you're dealing a lot with bytes rather than strings, etc.

This is related to #9806 in that it will make the way toward removing ToStr plainer.

bors added a commit that referenced this issue Oct 19, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Dec 1, 2022
Add allow-mixed-uninlined-format-args config

Implement `allow-mixed-uninlined-format-args` config param to change the behavior of the `uninlined_format_args` lint. Now it is a part of `style` per [Zulip chat](https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/.60uninlined_format_args.60.20category), and won't propose inlining in case of a mixed usage, e.g. `print!("{} {}", var, 1+2)`. If the user sets `allow-mixed-uninlined-format-args` config param to `false`, the lint would behave like it did before -- proposing to inline args even in the mixed case.

---

changelog: [`uninlined_format_args`]: Added a new config `allow-mixed-uninlined-format-args` to allow the lint, if only some arguments can be inlined
[rust-lang#9865](rust-lang/rust-clippy#9865)
changelog: Moved [`uninlined_format_args`] to `style` (Now warn-by-default)
[rust-lang#9865](rust-lang/rust-clippy#9865)
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

Successfully merging a pull request may close this issue.

1 participant