Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Sky9x opened this issue Apr 15, 2025 · 1 comment · May be fixed by #139316
Open

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

Sky9x opened this issue Apr 15, 2025 · 1 comment · May be fixed by #139316
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Sky9x
Copy link
Contributor

Sky9x commented Apr 15, 2025

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
@Sky9x Sky9x added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 15, 2025
@Sky9x

This comment has been minimized.

@rustbot rustbot added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Apr 15, 2025
@Sky9x Sky9x changed the title write!: rustc suggests editing the sysroot Missing use std::fmt::Write with write!: rustc suggests editing the sysroot Apr 15, 2025
@Sky9x Sky9x 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 Apr 15, 2025
@jieyouxu jieyouxu added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants