Skip to content

format! should support multiple formats for one argument #9456

Closed
@chris-morgan

Description

@chris-morgan

Take something like the following format string:

format!("{a:?} is {a}", a = "foo\u2014bar")

I expect this to produce the string "foo\u2014bar" is foo—bar.

However, what it actually produces is "foo\u2014bar" is "foo\u2014bar"; the behaviour of the second placeholder has changed from using Default to using Poly.

If I make the type difference explicit with the format string "{a:?} is {a:s}", then I get a compilation error, which, although not what I wanted, at least tells me I can't do what I'm trying to do:

0.rs:2:50: 2:64 error: argument redeclared with type `s` when it was previously `?`
0.rs:2     println!("a is {a:?} (literally, {a:s})", a = "foo\u2014bar");
                                                         ^~~~~~~~~~~~~~
error: aborting due to previous error

My problem is with the implicit change of behaviour for named arguments from using Default to using what was done last. This distinctly confused me when I came across it. If using an argument with different formatting constraints is not possible (I don't see why this constraint is there, truth to tell) then I believe the unspecified format should still be interpreted as Default rather than what-was-done-last, and an error raised rather than this surprising behaviour.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-syntaxextArea: Syntax extensions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions