Skip to content

Missed optimization for format whose argument is a known static string #144328

@jrose-signal

Description

@jrose-signal
pub fn test() -> String {
    let input = "abc";
    format!("pre:{}", input)
}

I expected/hoped this would be optimized down to "pre:abc".to_string(), as it does when a string literal is written inline as an argument to format!. However, the assembly output shows "abc" and "pre:" as separate strings. (Note that in the inline case, there is no string at all; the optimized form inlines the string data as integer constants.)

https://play.rust-lang.org/?version=nightly&mode=release&edition=2024&gist=111a73ab4d9631b03ce1345b1dfe12f4

(Occurs in both stable 1.88.0 and nightly 2025-07-21.)

Motivated by using std::any::type_name for logging and being mildly surprised it wasn't statically resolved, though I realize that may have a few layers beyond this one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-fmtArea: `core::fmt`C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler 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