Skip to content

print! and println! (and other similar macro pairs) accept differing types of "format strings" #47441

Closed
@shepmaster

Description

@shepmaster

println! accepts non-string literals as the format string:

fn main() {
    println!(12.3);
}

print! does not:

fn main() {
    print!(12.3);
}
error: format argument must be a string literal.
 --> src/main.rs:2:12
  |
2 |     print!(12.3);
  |            ^^^^

This is because println! is implemented as calling print! with the result of concat!:

https://github.com/rust-lang/rust/blob/1.23.0/src/libstd/macros.rs#L150-L154

print! just calls format_args:

https://github.com/rust-lang/rust/blob/1.23.0/src/libstd/macros.rs#L119-L121

However, concat! stringifies its argument:

https://github.com/rust-lang/rust/blob/1.23.0/src/libsyntax_ext/concat.rs#L32-L54

This means that the "format string" can be any of:

  • string literal
  • floating point literal
  • character literal
  • integer literal
  • boolean literal

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions