Skip to content

Missing use std::fmt::Write; with write!: rustc suggests editing the sysroot #139830

@Sky9x

Description

@Sky9x
Contributor

Code

playground

fn main() {
    let mut buf = String::new();
    _ = write!(buf, "foo");
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0599]: cannot write into `String`
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    |
   ::: /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    = help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
    |
1   + use std::fmt::Write;
    |
help: there is a method `write_str` with a similar name
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:713:14
    |
713 -         $dst.write_fmt($crate::format_args!($($arg)*))
713 +         $dst.write_str($crate::format_args!($($arg)*))
    |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0599]: cannot write into `String`
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    |
   ::: /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:218:8
    |
218 |     fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
    |        --------- the method is available for `String` here
    |
note: must implement `io::Write`, `fmt::Write`, or have a `write_fmt` method
   --> src/main.rs:3:16
    |
3   |     _ = write!(buf, "foo");
    |                ^^^
    = help: items from traits can only be used if the trait is in scope
help: trait `Write` which provides `write_fmt` is implemented but not in scope; perhaps you want to import it
    |
1   + use std::fmt::Write;
    |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Rationale and extra context

Editing libstd is not a solution for a missing trait import

Rust Version

rustc 1.88.0-nightly (092a284ba 2025-04-13)
binary: rustc
commit-hash: 092a284ba0421695f2032c947765429fd7095796
commit-date: 2025-04-13
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Apr 15, 2025
Sky9x

Sky9x commented on Apr 15, 2025

@Sky9x
Author
added
A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.
on Apr 15, 2025
changed the title [-]`write!`: rustc suggests editing the sysroot[/-] [+]Missing `use std::fmt::Write` with `write!`: rustc suggests editing the sysroot[/+] on Apr 15, 2025
changed the title [-]Missing `use std::fmt::Write` with `write!`: rustc suggests editing the sysroot[/-] [+]Missing `use std::fmt::Write;` with `write!`: rustc suggests editing the sysroot[/+] on Apr 15, 2025
added
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
on Apr 15, 2025
added a commit that references this issue on Jun 23, 2025

Rollup merge of rust-lang#142873 - Urgau:issue-139830, r=BoxyUwU

8ba8f1e
added a commit that references this issue on Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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

      Participants

      @jieyouxu@rustbot@Sky9x

      Issue actions

        Missing `use std::fmt::Write;` with `write!`: rustc suggests editing the sysroot · Issue #139830 · rust-lang/rust