rust-lang / rust-lang/rust

Missed optimization for format whose argument is a known static string

Open
#144,328 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-fmt C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Rust example in the issue and compare its release assembly with the inline-literal form on stable and nightly. Trace how format! handles arguments and whether the known static string is propagated. Done means the local-string form receives the same optimization as the inline form, with a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.