{"id":13437595,"url":"https://github.com/rust-lang/rustfmt","last_synced_at":"2025-05-12T22:35:44.098Z","repository":{"id":28317643,"uuid":"31830551","full_name":"rust-lang/rustfmt","owner":"rust-lang","description":"Format Rust code","archived":false,"fork":false,"pushed_at":"2025-04-12T16:40:58.000Z","size":15299,"stargazers_count":6259,"open_issues_count":829,"forks_count":925,"subscribers_count":47,"default_branch":"master","last_synced_at":"2025-05-05T17:21:48.585Z","etag":null,"topics":["codeformatter","formatter","rust","rustfmt"],"latest_commit_sha":null,"homepage":"https://rust-lang.github.io/rustfmt/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rust-lang.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"Contributing.md","funding":null,"license":"LICENSE-APACHE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-03-07T22:51:56.000Z","updated_at":"2025-05-05T15:12:05.000Z","dependencies_parsed_at":"2023-10-02T19:51:12.042Z","dependency_job_id":"27a62d41-a70d-4239-8b0c-d7b7ad5ba271","html_url":"https://github.com/rust-lang/rustfmt","commit_stats":{"total_commits":3978,"total_committers":461,"mean_commits":8.629067245119305,"dds":0.6814982403217698,"last_synced_commit":"34f9ca28f2f4ae3ac462c6dcd862b513e107e459"},"previous_names":["rust-lang-nursery/rustfmt"],"tags_count":127,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-lang%2Frustfmt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-lang%2Frustfmt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-lang%2Frustfmt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-lang%2Frustfmt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rust-lang","download_url":"https://codeload.github.com/rust-lang/rustfmt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253835005,"owners_count":21971720,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codeformatter","formatter","rust","rustfmt"],"created_at":"2024-07-31T03:00:58.572Z","updated_at":"2025-05-12T22:35:44.046Z","avatar_url":"https://github.com/rust-lang.png","language":"Rust","readme":"# rustfmt [![linux](https://github.com/rust-lang/rustfmt/actions/workflows/linux.yml/badge.svg?event=push)](https://github.com/rust-lang/rustfmt/actions/workflows/linux.yml) [![mac](https://github.com/rust-lang/rustfmt/actions/workflows/mac.yml/badge.svg?event=push)](https://github.com/rust-lang/rustfmt/actions/workflows/mac.yml) [![windows](https://github.com/rust-lang/rustfmt/actions/workflows/windows.yml/badge.svg?event=push)](https://github.com/rust-lang/rustfmt/actions/workflows/windows.yml) [![crates.io](https://img.shields.io/crates/v/rustfmt-nightly.svg)](https://crates.io/crates/rustfmt-nightly)\n\nA tool for formatting Rust code according to style guidelines.\n\nIf you'd like to help out (and you should, it's a fun project!), see\n[Contributing.md](Contributing.md) and our [Code of\nConduct](CODE_OF_CONDUCT.md).\n\nYou can use rustfmt in Travis CI builds. We provide a minimal Travis CI\nconfiguration (see [here](#checking-style-on-a-ci-server)).\n\n## Quick start\n\nYou can run `rustfmt` with Rust 1.24 and above.\n\n### On the Stable toolchain\n\nTo install:\n\n```sh\nrustup component add rustfmt\n```\n\nTo run on a cargo project in the current working directory:\n\n```sh\ncargo fmt\n```\n\n### On the Nightly toolchain\n\nFor the latest and greatest `rustfmt`, nightly is required.\n\nTo install:\n\n```sh\nrustup component add rustfmt --toolchain nightly\n```\n\nTo run on a cargo project in the current working directory:\n\n```sh\ncargo +nightly fmt\n```\n\n## Limitations\n\nRustfmt tries to work on as much Rust code as possible. Sometimes, the code\ndoesn't even need to compile! In general, we are looking to limit areas of\ninstability; in particular, post-1.0, the formatting of most code should not\nchange as Rustfmt improves. However, there are some things that Rustfmt can't\ndo or can't do well (and thus where formatting might change significantly,\neven post-1.0). We would like to reduce the list of limitations over time.\n\nThe following list enumerates areas where Rustfmt does not work or where the\nstability guarantees do not apply (we don't make a distinction between the two\nbecause in the future Rustfmt might work on code where it currently does not):\n\n* a program where any part of the program does not parse (parsing is an early\n  stage of compilation and in Rust includes macro expansion).\n* Macro declarations and uses (current status: some macro declarations and uses\n  are formatted).\n* Comments, including any AST node with a comment 'inside' (Rustfmt does not\n  currently attempt to format comments, it does format code with comments inside, but that formatting may change in the future).\n* Rust code in code blocks in comments.\n* Any fragment of a program (i.e., stability guarantees only apply to whole\n  programs, even where fragments of a program can be formatted today).\n* Code containing non-ascii unicode characters (we believe Rustfmt mostly works\n  here, but do not have the test coverage or experience to be 100% sure).\n* Bugs in Rustfmt (like any software, Rustfmt has bugs, we do not consider bug\n  fixes to break our stability guarantees).\n\n\n## Running\n\nYou can run Rustfmt by just typing `rustfmt filename` if you used `cargo\ninstall`. This runs rustfmt on the given file, if the file includes out of line\nmodules, then we reformat those too. So to run on a whole module or crate, you\njust need to run on the root file (usually mod.rs or lib.rs). Rustfmt can also\nread data from stdin. Alternatively, you can use `cargo fmt` to format all\nbinary and library targets of your crate.\n\nYou can run `rustfmt --help` for information about available arguments.\nThe easiest way to run rustfmt against a project is with `cargo fmt`. `cargo fmt` works on both\nsingle-crate projects and [cargo workspaces](https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html).\nPlease see `cargo fmt --help` for usage information.\n\nYou can specify the path to your own `rustfmt` binary for cargo to use by setting the`RUSTFMT` \nenvironment variable. This was added in v1.4.22, so you must have this version or newer to leverage this feature (`cargo fmt --version`)\n\n### Running `rustfmt` directly\n\nTo format individual files or arbitrary codes from stdin, the `rustfmt` binary should be used. Some\nexamples follow:\n\n- `rustfmt lib.rs main.rs` will format \"lib.rs\" and \"main.rs\" in place\n- `rustfmt` will read a code from stdin and write formatting to stdout\n  - `echo \"fn     main() {}\" | rustfmt` would emit \"fn main() {}\".\n\nFor more information, including arguments and emit options, see `rustfmt --help`.\n\n### Verifying code is formatted\n\nWhen running with `--check`, Rustfmt will exit with `0` if Rustfmt would not\nmake any formatting changes to the input, and `1` if Rustfmt would make changes.\nIn other modes, Rustfmt will exit with `1` if there was some error during\nformatting (for example a parsing or internal error) and `0` if formatting\ncompleted without error (whether or not changes were made).\n\n\n\n## Running Rustfmt from your editor\n\n* [Vim](https://github.com/rust-lang/rust.vim#formatting-with-rustfmt)\n* [Emacs](https://github.com/rust-lang/rust-mode)\n* [Sublime Text 3](https://packagecontrol.io/packages/RustFmt)\n* [Atom](atom.md)\n* [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)\n* [IntelliJ or CLion](intellij.md)\n\n\n## Checking style on a CI server\n\nTo keep your code base consistently formatted, it can be helpful to fail the CI build\nwhen a pull request contains unformatted code. Using `--check` instructs\nrustfmt to exit with an error code if the input is not formatted correctly.\nIt will also print any found differences. (Older versions of Rustfmt don't\nsupport `--check`, use `--write-mode diff`).\n\nA minimal Travis setup could look like this (requires Rust 1.31.0 or greater):\n\n```yaml\nlanguage: rust\nbefore_script:\n- rustup component add rustfmt\nscript:\n- cargo build\n- cargo test\n- cargo fmt --all -- --check\n```\n\nSee [this blog post](https://medium.com/@ag_dubs/enforcing-style-in-ci-for-rust-projects-18f6b09ec69d)\nfor more info.\n\n## How to build and test\n\n`cargo build` to build.\n\n`cargo test` to run all tests.\n\nTo run rustfmt after this, use `cargo run --bin rustfmt -- filename`. See the\nnotes above on running rustfmt.\n\n\n## Configuring Rustfmt\n\nRustfmt is designed to be very configurable. You can create a TOML file called\n`rustfmt.toml` or `.rustfmt.toml`, place it in the project or any other parent\ndirectory and it will apply the options in that file. See `rustfmt\n--help=config` for the options which are available, or if you prefer to see\nvisual style previews, [GitHub page](https://rust-lang.github.io/rustfmt/).\n\nBy default, Rustfmt uses a style which conforms to the [Rust style guide][style\nguide] that has been formalized through the [style RFC\nprocess][fmt rfcs].\n\nConfiguration options are either stable or unstable. Stable options can always\nbe used, while unstable ones are only available on a nightly toolchain, and opt-in.\nSee [GitHub page](https://rust-lang.github.io/rustfmt/) for details.\n\n### Rust's Editions\n\nThe `edition` option determines the Rust language edition used for parsing the code. This is important for syntax compatibility but does not directly control formatting behavior (see [Style Editions](#style-editions)).\n\nWhen running `cargo fmt`, the `edition` is automatically read from the `Cargo.toml` file. However, when running `rustfmt` directly, the `edition` defaults to 2015. For consistent parsing between rustfmt and `cargo fmt`, you should configure the `edition` in your `rustfmt.toml` file:\n\n```toml\nedition = \"2018\"\n```\n\n### Style Editions\n\nThis option is inferred from the [`edition`](#rusts-editions) if not specified.\n\nSee [Rust Style Editions] for details on formatting differences between style editions.\nrustfmt has a default style edition of `2015` while `cargo fmt` infers the style edition from the `edition` set in `Cargo.toml`. This can lead to inconsistencies between `rustfmt` and `cargo fmt` if the style edition is not explicitly configured.\n\nTo ensure consistent formatting, it is recommended to specify the `style_edition` in a `rustfmt.toml` configuration file. For example:\n\n```toml\nstyle_edition = \"2024\"\n```\n[Rust Style Editions]: https://doc.rust-lang.org/nightly/style-guide/editions.html?highlight=editions#rust-style-editions\n[Rust Style Guide]: https://doc.rust-lang.org/nightly/style-guide/\n[RFC 3338]: https://rust-lang.github.io/rfcs/3338-style-evolution.html\n\n## Tips\n\n* To ensure consistent parsing between `cargo fmt` and `rustfmt`, you should configure the [`edition`](#rusts-editions) in your `rustfmt.toml` file.\n* To ensure consistent formatting between `cargo fmt` and `rustfmt`, you should configure the [`style_edition`](#style-editions) in your `rustfmt.toml` file.\n\n* For things you do not want rustfmt to mangle, use `#[rustfmt::skip]`\n* To prevent rustfmt from formatting a macro or an attribute,\n  use `#[rustfmt::skip::macros(target_macro_name)]` or\n  `#[rustfmt::skip::attributes(target_attribute_name)]`\n\n  Example:\n\n    ```rust\n    #![rustfmt::skip::attributes(custom_attribute)]\n\n    #[custom_attribute(formatting , here , should , be , Skipped)]\n    #[rustfmt::skip::macros(html)]\n    fn main() {\n        let macro_result1 = html! { \u003cdiv\u003e\n    Hello\u003c/div\u003e\n        }.to_string();\n    ```\n* When you run rustfmt, place a file named `rustfmt.toml` or `.rustfmt.toml` in\n  target file directory or its parents to override the default settings of\n  rustfmt. You can generate a file containing the default configuration with\n  `rustfmt --print-config default rustfmt.toml` and customize as needed.\n* After successful compilation, a `rustfmt` executable can be found in the\n  target directory.\n* If you're having issues compiling Rustfmt (or compile errors when trying to\n  install), make sure you have the most recent version of Rust installed.\n\n* You can change the way rustfmt emits the changes with the --emit flag:\n\n  Example:\n\n  ```sh\n  cargo fmt -- --emit files\n  ```\n\n  Options:\n\n  | Flag |Description| Nightly Only |\n  |:---:|:---:|:---:|\n  | files | overwrites output to files | No |\n  | stdout | writes output to stdout | No |\n  | coverage | displays how much of the input file was processed | Yes |\n  | checkstyle | emits in a checkstyle format | Yes |\n  | json | emits diffs in a json format | Yes |\n\n## License\n\nRustfmt is distributed under the terms of both the MIT license and the\nApache License (Version 2.0).\n\nSee [LICENSE-APACHE](LICENSE-APACHE) and [LICENSE-MIT](LICENSE-MIT) for details.\n\n[rust]: https://github.com/rust-lang/rust\n[fmt rfcs]: https://github.com/rust-dev-tools/fmt-rfcs\n[style guide]: https://doc.rust-lang.org/nightly/style-guide/\n","funding_links":[],"categories":["Rust","Development tools","Supported Linters","Ecosystems","开发工具 Development tools","⛓️🧰🦀 web3 toolkit - rust edition","开发工具","Rust 程序设计","Tools","Programming Languages","虚拟化"],"sub_categories":["Web Servers","Formatters","[Rust](https://www.rust-lang.org/)","格式器 Formatters","Web 服务器","网络服务端 Web Servers","网络服务_其他","格式化工具"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frust-lang%2Frustfmt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frust-lang%2Frustfmt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frust-lang%2Frustfmt/lists"}