Clippy, rust format, and Cargo.toml

Apologies as I am probably missing the obvious, but I can't see it.
In Cargo.toml I have

[package]
name = "library"
version = "0.1.0"
edition = "2024"

Running Clippy, it nicely told me to cascade if Let after another condition, eg:

            if !guistate.author_state.working_name.is_empty() 
                && let Some(lib) = &mut guistate.library {

However, then when I run rust format, it says:

error: let chains are only allowed in Rust 2024 or later
  --> author_gui.rs:99:20
   |
99 |                 && let Some(lib) = &mut guistate.library {
   |   

which while presumably a true statement does not appear to be an error, since I am running Rust 2024??
Puzzled,
Joel

rustfmt doesn't know about Cargo or your config through cargo. You need to tell it explicitly:

rustfmt --edition 2024 author_gui.rs

This is basically what cargo fmt does under the hood.
See also: Rustfmt: Style edition - The Rust Edition Guide

I am actually using emacs Rust binding C-c C-f to invoke formating. which it tells me is (rust-format-buffer). Any idea how I tell that --edition 2024?

No idea, I don't use emacs. But see the docs I linked: you should be able to define it in a rustfmt.toml too.

I tried putting a rustfmt.toml file next to the Cargo.toml, with the line from the manual.
I then went to the directory with the file giving me trouble, and ran rustfmt on the file. It gave me the same error. Do I need to put rustfmt.toml in the src directory? In src plus every subdirectory? (Not that big a deal, just confusing.)
Thanks,
Joel

Found it. I needed to put in a line slightly different from what the doc page seemed to suggest. The doc talked about a line

style-edition = "2024"
``
but of course what I needed was

edition = "2024"

Thanks for the pointer,
Joel

Anyone using the emacs invocation of formating? At this point, if I run rustfmt from the command line, it doesn't complain. But if I have the file in emacs, and type C-c C-f, I get the version complaint. Do I not mroe copies of rustfmt.toml because of how emacs runs it? Do I need to tell the emacs command somehow --edition 2024?
Joel

PS: jer, thanks for the prompt response that covered the non-emacs part of the problem.

IIRC rustfmt will search up the directory tree. I always put my rustfmt.toml in the crate/workspace root, and it is automatically picked up whichever subcrate I am working in.

Could you check the emacs "plugin" that runs rustfmt to see if it sets the current directory before running the command? Just a wild guess, but maybe the plugin just pipes the source through rustfmt, and it doesn't bother setting the current directory. (Though to be honest that would surprise me).

As far as I know, rustfmt itself is a compiled lisp thing, so I am not sure how to check it. BBut I tried checking the other way. I copied rustfmt.toml into the directory with the file giving me trouble. And then told emacs to run rustfmt. And got the same error.

Trying searching for a remedy, I even tried adding

(setq rust-rustfmt-bin "rustfmt --edition 2024")

to my init.el. DIdn't help.

When I search I find references to a rust-rustfmt-switches rust-rustfmt-buffer variable defined in rust-mode.el. I'm not an emacs user so I can't tell you how to set it.

Yes, that is the emacs lisp function that is used to invoke rustfmt. I have not found any way to specify the --edition parameter for it. I have also been unable to figure out why it is ignore rustfmt.toml. I am starting to conclude that emacs use for rust is less common than I had hoped.

First time I see an advantage I do not use Cargo. The mentioned problem simply doesn't exist in my case.

Here is a snippet how do I configure my IDE: RDS

At a guess, the problem is my emacs config, not cargo. Running cargo fmt directly works fine. With the suggested changes, even running rustfmt directly works.
Yours,
Joel

Awesome, if Cargo use as a middle man, everything works too.

I simply have ~/.config/rustfmt/rustfmt.toml with

edition = "2024"

I will have to try putting the file in that location, since my home as seen by emacs is not the project base as seen by Rust. Thanks.

I tried sprinkling rustfmt.toml files on .config/rustfmt/, in .emacs.d/, and in .emacs.d/rustfmt/. Didn't do the trick. I wonder where I am supposed to put it, or if I need a line in my .init.el to tell it where to look?
Thanks,
Joel

Are you on Linux?

@Joel_Halpern I use Emacs as well (on macOS), and placing a rustfmt.toml next to the Cargo.toml containing

edition = "2024"

works for me. I'm using Emacs 30.2 and rustic-mode 20260216.440. I also have:

  • rust-rustfmt-bin set to "rustfmt"
  • rustic-rustfmt-config-alist set to nil
  • rust-rustfmt-switches set to nil
  • rustic-rustfmt-args set to nil
  • and the same for all other related rustfmt variables AFAICT.

Maybe if you can figure out the differences from this setup we'll be able to figure it out?

For a concrete example, here's the latest project I'm working on – the details aren't important, but it has files containing async and the aforementioned rustfmt.toml. If you clone that, can you successfully format src/client.rs or other such files?

I am on Windows, which I presume is part of the problem.