{
  "description": "Browse aymeric's, an independent, searchable archive of readable snapshots from followed feeds, preserved in Git with aggr.",
  "feed_url": "https://aggr.aymericbeaumet.com/sources/google.github.io/feed.json",
  "home_page_url": "https://aggr.aymericbeaumet.com/sources/google.github.io/",
  "items": [
    {
      "authors": [
        {
          "name": "kaycebasques"
        }
      ],
      "content_html": "<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#rust-style-guide\">Rust Style Guide</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#background\">Background</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#scope\">Scope</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#goals-of-the-style-guide\">Goals of the style guide</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#follow-public-rust-style-rules\">Follow public Rust style rules</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#apply-api-design-rules-in-context\">Apply API design rules in context</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#code-organization\">Code organization</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-small-crates-chrome\">Prefer small crates [chrome]</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-flat-file-layout-for-rust_library-sources-chrome\">Use flat file layout for <code>rust_library</code> sources [chrome]</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#name-source-files-and-leaf-modules-after-the-primary-user-defined-item\">Name source files and leaf modules after the primary user-defined item</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-implement-apis-in-non-leaf-modules\">Don’t implement APIs in non-leaf modules</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-use-glob-imports\">Don’t use glob imports</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#a-use-statement-should-import-items-from-one-module\">A <code>use</code> statement should import items from one module</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-locally-scoped-trait-imports\">Prefer locally-scoped trait imports</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#qualify-calls-to-top-level-functions-with-a-module-name\">Qualify calls to top-level functions with a module name</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#type-names-should-be-imported-and-used-unqualified\">Type names should be imported and used unqualified</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-the-appropriate-import-mechanism-chrome\">Use the appropriate import mechanism [chrome]</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#code-formatting-and-aesthetics\">Code formatting and aesthetics</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-the-appropriate-formatting-tool\">Use the appropriate formatting tool</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#opting-out-of-automatic-formatting\">Opting out of automatic formatting</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#ownership-and-borrowing\">Ownership and borrowing</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#avoid-rc-and-arc\">Avoid <code>Rc</code> and <code>Arc</code></a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-borrows-over-deep-copies\">Prefer borrows over deep copies</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#function-inputs-and-outputs\">Function inputs and outputs</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#accept-flexible-references-for-immutable-data\">Accept flexible references for immutable data</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#iterators-vs-owned-collections-in-return-types\">Iterators vs. owned collections in return types</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#iterators-vs-slices-in-function-parameters\">Iterators vs. slices in function parameters</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#accepting-into\">Accepting <code>Into</code></a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-enum-over-bool-in-apis\">Prefer <code>enum</code> over <code>bool</code> in APIs</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#reporting-errors\">Reporting errors</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#types\">Types</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#newtypes-versus-type-aliases\">Newtypes versus type aliases</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-named-fields-over-tuples-and-non-newtype-tuple-structs\">Prefer named fields over tuples and non-newtype tuple structs</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#traits\">Traits</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#add-trait-implementations-when-they-become-necessary\">Add trait implementations when they become necessary</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#implement-known-standard-library-traits-instead-of-custom-methods\">Implement known standard library traits instead of custom methods</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-derive-if-possible\">Use <code>#[derive()]</code> if possible</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#generics-versus-dyn-trait\">Generics versus <code>dyn Trait</code></a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#when-to-define-a-trait-versus-an-inherent-implementation\">When to define a trait versus an inherent implementation</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#extension-traits\">Extension traits</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#specific-guidance-for-important-traits\">Specific guidance for important traits</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#pair-important-from-impls-with-explicit-constructor-methods\">Pair important <code>From</code> impls with explicit constructor methods</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#options-structs-should-implement-default\">Options structs should implement Default</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#concurrency\">Concurrency</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#handling-poison\">Handling poison</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-use-async--await-chrome\">Don’t use <code>async</code> / <code>.await</code> [chrome]</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-start-new-threads-through-the-rust-standard-library-chrome\">Don’t start new threads through the Rust standard library [chrome]</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-fuchsia_syncmutex-fuchsia\">Use <code>fuchsia_sync::Mutex</code> [Fuchsia]</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#macros\">Macros</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#naming\">Naming</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#naming-methods\">Naming methods</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#naming-lifetimes\">Naming lifetimes</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#naming-generic-parameters\">Naming generic parameters</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#naming-extension-traits\">Naming extension traits</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#naming-initialization-functions\">Naming initialization functions</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#avoid-abbreviations\">Avoid abbreviations</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#when-and-how-to-write-documentation\">When and how to write documentation</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#other-rust-features\">Other Rust features</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#unsafe-code\">Unsafe code</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#no-unstable-features\">No unstable features</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#getters-and-setters\">Getters and setters</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#more-on-accessors-that-accept-closures\">More on accessors that accept closures</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#implement-expected-api-sets-together\">Implement expected API sets together</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#do-not-access-items-marked-dochidden-that-you-dont-own\">Do not access items marked <code>#[doc(hidden)]</code> that you don’t own</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-const-functions-responsibly\">Use <code>const</code> functions responsibly</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#avoid-overloading-for-non-mathematical-purposes\">Avoid overloading for non-mathematical purposes</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#apply-non_exhaustive-to-publicly-exposed-enums\">Apply <code>#[non_exhaustive]</code> to publicly-exposed enums</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#apply-non_exhaustive-to-public-structs-with-public-fields\">Apply <code>#[non_exhaustive]</code> to public structs with public fields</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#must_use-on-functions\"><code>#[must_use]</code> on functions</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#must_use-on-types\"><code>#[must_use]</code> on types</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#do-not-run-code-before-main\">Do not run code before <code>main()</code></a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#implementation-concerns\">Implementation concerns</a>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-overuse-type-inference\">Don’t overuse type inference</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-pattern-matching-over-check-then-unwrap\">Prefer pattern matching over check-then-unwrap</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-expect-over-unwrap\">Prefer <code>expect</code> over <code>unwrap</code></a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-omit-patterns-on-non_exhaustive-enums-when-matching\">Don’t omit patterns on <code>non_exhaustive</code> enums when matching</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-context-strings-to-clarify-assertions\">Use context strings to clarify assertions</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-use-iterator-combinator-chains-when-direct-iteration-is-clearer\">Don’t use iterator combinator chains when direct iteration is clearer</a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#dont-call-paniccatch_unwind\">Don’t call <code>panic::catch_unwind</code></a></li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#prefer-format--write-for-concatenation-and-substitution\">Prefer <code>format!</code> / <code>write!</code> for concatenation and substitution</a></li>\n</ul>\n</li>\n<li><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#use-the-right-per-codebase-test-framework\">Use the right per-codebase test framework</a></li>\n</ul>\n</li>\n</ul>\n<p>This document is a <em>style guide</em> for the usage of the Rust programming language at Google. In this context, “style” refers to more than just formatting; it includes guidance on API and implementation design.</p>\n<p>Rust is used in many places, including <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://opensource.google/documentation/reference/glossary#google3\">google3</a>, Android, ChromeOS, Fuchsia, and Chrome, and we’d like for Rust usage at Google to be consistent both within and across codebases. In addition to bringing consistency to our Rust code, style guidance also helps establish a standard of quality.</p>\n<h2>Background</h2>\n<h3>Scope</h3>\n<p>The current scope of authority of the rules outlined in this guide:</p>\n<ul>\n<li><strong>In Android, Chromium, and other codebases</strong>: This style guide has been developed in consultation with other codebases, and they intend to start applying it. These codebases may have their own rules about how strictly this is expected to be applied. Expect more information here as these codebases adopt this guide.</li>\n</ul>\n<h3>Goals of the style guide</h3>\n<p>By and large, we have this document for <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/cppguide.html#Goals\">the same reasons we have a C++ style guide</a>.</p>\n<p>Most style rules come from one or more of the goals listed below.</p>\n<p>Style rules should prioritize readability and maintainability over writeability</p>\n<p>We expect our code to be around for long periods of time, and we consider a little bit of extra effort applied during code writing to be worth it in the long run if the style rule improves readability.</p>\n<p>Style rules should favor better automation</p>\n<p>Large-Scale Changes (LSCs) are common at Google, and consistency helps make LSCs easier to carry out.</p>\n<p>Style rules should be consistent with the wider Rust ecosystem where possible</p>\n<p>The broader Rust ecosystem already has a pretty good amount of consistency in style, and we should rely on that where possible to reduce surprise and make it less work to learn Google’s Rust style.</p>\n<p>Style rules should pull their weight</p>\n<p>Every Rust developer and reviewer at Google has to keep these style rules in mind: as such, we should be careful to include rules that have a tangible benefit.</p>\n<p>Style rules may help avoid minor style disagreements</p>\n<p>There are many cases where reasonable people may disagree about a style, and we expect that disagreement to show up often on CLs. Having a style rule helps avoid having to rehash this each time. Rules in this category that do not have other benefits should be kept to a minimum.</p>\n<h2>Follow public Rust style rules</h2>\n<p>Follow the official public Rust style rules to the extent they don’t conflict with this style guide.</p>\n<ul>\n<li>\n<p><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/nightly/style-guide/\">Rust Style Guide</a></p>\n<ul>\n<li>Most code formatting conventions are enforced automatically by rustfmt.</li>\n</ul>\n</li>\n<li>\n<p><a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/api-guidelines/\">Rust API Guidelines</a></p>\n</li>\n</ul>\n<p>A “conflict” would be a case where this style guide suggests a pattern that is discouraged, or counter to the primary encouraged pattern in the Rust style guide.</p>\n<p>Why does the guidance in this style guide deviate from regular Rust style rules?</p>\n<p>Most style guides have three goals: consistency, readability, and encouraging good API design. Regardless of the specific choices made, it helps everyone to have these choices made beforehand to limit bikeshedding.</p>\n<p>This style guide shares those goals, but it also has an additional goal not seen as often: smoothing the way for Large-Scale Changes (LSCs). Large codebases like many of the ones at Google occasionally need to run codebase-spanning refactorings that are assisted by tooling: for this to work well, the code should be something tooling has an easy time working with. Consistency helps for this, but so does having deliberate refactor-friendly patterns.</p>\n<h2>Apply API design rules in context</h2>\n<p>When applying rules that relate to API design, you should distinguish two types of code:</p>\n<ul>\n<li>\n<p>Library code</p>\n<ul>\n<li>Intended for use by other projects (applications or other libraries).</li>\n<li>Solves a relatively general problem.</li>\n</ul>\n</li>\n<li>\n<p>Application code</p>\n<ul>\n<li>Intended for use in a single application.</li>\n<li>Solves a more specific problem.</li>\n</ul>\n</li>\n</ul>\n<p>For <em>library code</em>, style guide rules that concern API design are binding.</p>\n<p>For <em>application code</em>, the rules on API design are a strong recommendation. As you apply them, keep in mind that since application code is written to solve a specific problem, it should generally favor simplicity and directness over broad reusability. Avoid premature generalization and abstraction; focus on what is required to meet the immediate need.</p>\n<h2>Code organization</h2>\n<p><em>Terminology:</em></p>\n<ul>\n<li>\n<p>A <em>crate</em> is a unit of compilation. A crate always contains a nameless Rust module, the “root module”.</p>\n</li>\n<li>\n<p>A Rust <em>module</em> is a unit of code organization within a crate. Rust modules are used to control visibility of names. Modules can be nested. They can also be used to split code across files and directories.</p>\n</li>\n</ul>\n<p>The following sections convey that in Chromium, it is preferable to <strong>not</strong> make use of all possibilities of splitting modules across files.</p>\n<h3>Prefer small crates [chrome]</h3>\n<p>Prefer small crates in Chromium; ideally, but not necessarily, a single file (focusing on one abstraction). If your code can be split up into small crates, do so.</p>\n<p>A crate corresponds to a single <code>rust_static_library</code> (chrome) rule, which may contain one or more <code>.rs</code> files.</p>\n<p>Additional considerations:</p>\n<ul>\n<li>\n<p>The root module is allowed to <code>re-export</code> public APIs from <code>leaf submodules</code>, making submodules an implementation detail of the crate (as users don’t need to know about these submodules).</p>\n</li>\n<li>\n<p>This rule about small crates applies equally to crates with and without submodules. First, evaluate whether the APIs need to be in the same crate, and only then consider whether submodules could help organize the APIs.</p>\n</li>\n<li>\n<p>If you decide to use submodules, follow the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#structure\">expected file structure</a>.</p>\n</li>\n</ul>\n<p>Larger crates are allowed when <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://smallcultfollowing.com/babysteps/blog/2015/01/14/little-orphan-impls/\">trait impl coherence rules</a> force multiple types and traits to be included in one library, or if you’re building a large foundational library.</p>\n<p>Rationale</p>\n<p>Fine-grained library targets are recommended in Chromium due to the massive size of our codebase, non-local effects of adding dependencies in the build graph, and to improve performance of parallel compilation and build caching.</p>\n<h3>Use flat file layout for <code>rust_library</code> sources [chrome]</h3>\n<p>In Chromium <code>.rs</code> source files belonging to one <code>rust_library</code> (one Rust crate) must reside in a single directory.</p>\n<p>Don’t use subdirectories to organize files that belong to a single crate. If organizing one crate’s files into subdirectories feels necessary, this is a strong indication that the crate has grown <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#small-crates\">too large</a> and should be split.</p>\n<p>This crate layout is intentionally different from the conventional Cargo directory structure typically used in open-source repositories.</p>\n<p>Exception: Rust code in <code>//third_party</code> that is also built with <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/cargo/\">Cargo</a> should follow the conventional Cargo directory structure.</p>\n<p>Rationale</p>\n<p>The approach in Chromium is driven by the following factors:</p>\n<ul>\n<li>Based on the preference for <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#small-crates\">small crates</a>, most <code>rust_library</code> targets will only contain one source file.</li>\n<li>Because of the above point, creating a directory for each <code>rust_library</code> target would result in most directories containing only one source file.</li>\n<li>Multi-file <code>rust_library</code> targets should not create dedicated directories for submodules either; this ensures a consistent approach.</li>\n</ul>\n<h3>Name source files and leaf modules after the primary user-defined item</h3>\n<p>When a <code>leaf module</code> has a primary user-defined item (e.g., <code>Foo</code>), the module should be named after that item (e.g., <code>mod foo</code> and <code>foo.rs</code>).</p>\n<p>Leaf modules that are not primarily about any particular abstraction can be named as desired.</p>\n<p>Rationale</p>\n<p>This makes it easy to identify the content/purpose of a source file or leaf module at a glance.</p>\n<h3>Don’t implement APIs in non-leaf modules</h3>\n<p>The only items in non-leaf modules should be module declarations (not definitions) and <code>re-exports</code>.</p>\n<p>Note that the definition of <code>leaf modules</code> allows the following sub-modules: <code>mod test</code> and cross-language binding modules like <code>#[cxx::bridge]</code>.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Having a small amount of core “ties things together” implementation code in a non-leaf module may be fine. The goal is to not have a lot of code in these modules.</li>\n</ul>\n<p>Rationale</p>\n<p>This makes it easier to find things, and avoids mixing structure and contents.</p>\n<h3>Don’t use glob imports</h3>\n<p>Don’t use glob imports, except in the following situations where you may optionally do so at your own discretion:</p>\n<ul>\n<li>\n<p><code>use super::*</code> for inline modules</p>\n</li>\n<li>\n<p><code>use enum::*</code> in method bodies of enums, and functions that are primarily dealing with that enum</p>\n</li>\n<li>\n<p><code>pub use submodule::*</code> for non-leaf modules</p>\n</li>\n<li>\n<p>C++ interop: Since the same namespace (in C++) is a submodule in different Rust crates (in Rust), you may merge the namespaces into a private copy using glob imports. For example:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-module syntax-rust\"><span class=\"syntax-storage syntax-type syntax-module syntax-rust\">mod</span> <span class=\"syntax-entity syntax-name syntax-module syntax-rust\">absl</span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-storage syntax-modifier syntax-rust\">pub</span> <span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">status<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-path syntax-rust\">absl<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-keyword syntax-operator syntax-rust\">*</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n    <span class=\"syntax-storage syntax-modifier syntax-rust\">pub</span> <span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">strings<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-path syntax-rust\">absl<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-keyword syntax-operator syntax-rust\">*</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p>In this hypothetical example, you can use <code>absl::Status</code> and <code>absl::StrCat</code> from Rust, the same way you would from C++, instead of spelling them as <code>status::absl::Status</code> and <code>strings::absl::StrCat</code>.</p>\n<p>This must only be used for merging C++ namespaces, and the resulting merged module must not be public.</p>\n</li>\n</ul>\n<p>If using third-party crates that encourage a prelude, it is up to the reviewer’s discretion to allow it.</p>\n<p>This also means that defining preludes should be avoided.</p>\n<p>Rationale</p>\n<p>Glob imports increase toil for code janitors running LSCs, and make it harder to know where a type comes from in contexts without xrefs.</p>\n<h3>A <code>use</code> statement should import items from one module</h3>\n<p>This rule corresponds to the rustfmt setting <code>imports_granularity = Module</code>.</p>\n<p>Basically, there should not be any <code>::</code>’s inside the <code>{..}</code> block of a <code>use</code> statement. For example:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foo<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Foo<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foo<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-path syntax-rust\">bar<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Bar<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foo<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-path syntax-rust\">bar<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Baz<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foo<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Foo<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foo<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-path syntax-rust\">bar<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>Bar<span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span> Baz</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foo<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>Foo<span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span> <span class=\"syntax-meta syntax-path syntax-rust\">bar<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Bar</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<h3>Prefer locally-scoped trait imports</h3>\n<p>Import traits within function bodies, unless the trait’s methods are used extensively throughout the file.</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">foo</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-parameter syntax-rust\">x</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-storage syntax-type syntax-rust\">u8</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> Import the trait within a function, not at the module scope.\n</span>    <span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">std<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-path syntax-rust\">str<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>FromStr<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n\n    <span class=\"syntax-storage syntax-type syntax-rust\">let</span> string<span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-support syntax-type syntax-rust\">String</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> x.<span class=\"syntax-support syntax-function syntax-rust\">parse</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<h3>Qualify calls to top-level functions with a module name</h3>\n<p>To use top-level functions defined in other modules or crates, import their module and call them with <code>modulename::functionname()</code>. For example:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">std<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>mem<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n\n<span class=\"syntax-storage syntax-type syntax-rust\">let</span> x <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-meta syntax-path syntax-rust\">mem<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>transmute<span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>y</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">std<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-meta syntax-path syntax-rust\">mem<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>transmute<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n\n<span class=\"syntax-storage syntax-type syntax-rust\">let</span> x <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-support syntax-function syntax-rust\">transmute</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>y</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<p>Exceptions:</p>\n<ul>\n<li>\n<p>When importing functions with sufficiently disambiguated names, e.g., functions from C++ (where it is typical to call top-level (free) functions directly).</p>\n</li>\n<li>\n<p>In Chromium, when importing functions from <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#small-crates\">small, single-purpose first-party crates</a>.</p>\n</li>\n<li>\n<p>When the module name itself is prone to collisions. For example, when the module name is a C++ namespace name, and multiple crates define the same submodules.</p>\n</li>\n</ul>\n<p>Rationale</p>\n<p>Many Rust codebases consistently keep a style such that unqualified lowercase names are assumed local, so <code>foo()</code> can be assumed to be from the same module. This rule largely exists to avoid reviewer disagreements, the benefit is mostly consistency and a minor edge on reading code without xrefs.</p>\n<h3>Type names should be imported and used unqualified</h3>\n<p>By default, types and traits should be referenced in code without using their module path, e.g., code should use <code>Regex</code>, not <code>regex::Regex</code>, with a <code>use regex::Regex</code> at the top of the file or potentially in the relevant function.</p>\n<p>Exceptions:</p>\n<ul>\n<li>In cases where there are ambiguities, it is acceptable to directly reference types with common names like <code>io::Error</code>. It is also acceptable to rename (<code>use std::io::Error as IoError</code>).</li>\n<li>In cases where you have a large “group” of types, e.g. the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/index.html\"><code>hir module in rustc</code></a> which contains a large number of HIR types.</li>\n<li>In macros and generated code; where requiring a <code>use</code> is counterproductive.</li>\n<li>In code with lots of dependencies, where importing everything would lead to a very large import block with little benefit.</li>\n</ul>\n<p>Rationale</p>\n<p>This rule largely exists to avoid reviewer disagreements.</p>\n<h3>Use the appropriate import mechanism [chrome]</h3>\n<ul>\n<li>Chrome Use <code>chromium::import!()</code>.</li>\n</ul>\n<h2>Code formatting and aesthetics</h2>\n<h3>Use the appropriate formatting tool</h3>\n<p>Use the appropriate formatting tool (e.g., <code>git cl format</code> in Chromium).</p>\n<p>Configuration (for style guide authors)</p>\n<p>A codebase may choose to not apply the following <code>rustfmt</code> options if they care about building on stable and the option is unavailable on their stable Rust:</p>\n<ul>\n<li>\n<p>Maximum of 100 columns per line: <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rustfmt/?version=v1.6.0&amp;search=#max_width\"><code>max_width = 100</code></a></p>\n</li>\n<li>\n<p>Usage of lowercase alphabets in hex literals (<code>0xab123cdef</code> as opposed to <code>0xAB123CDEF</code>): <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rustfmt/?version=v1.6.0&amp;search=#hex_literal_case\"><code>hex_literal_case = Lower</code></a></p>\n</li>\n<li>\n<p>Macro matcher formatting: <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rustfmt/?version=v1.6.0&amp;search=#format_macro_matchers\"><code>format_macro_matchers = true</code></a></p>\n</li>\n<li>\n<p>Usage of escapes for invisible characters in literals. E.g., if you wish to have text containing a Zero-Width joiner, use <code>\\u{200D}</code>.</p>\n</li>\n</ul>\n<h3>Opting out of automatic formatting</h3>\n<p>You may opt out of automatic formatting using <code>#[rustfmt::skip]</code>, but only in the following situations:</p>\n<ul>\n<li><strong>Manual alignment</strong> – If you have a situation-specific reason to have a snippet of code line up in a particular way (e.g., multiple similar lines of code that you want to remain aligned for readability, or a matrix-constructing function).</li>\n<li><strong>Generated code</strong> (ideally, use a genrule instead).</li>\n</ul>\n<p>Important: Formatting should not be skipped for personal style choices.</p>\n<h2>Ownership and borrowing</h2>\n<h3>Avoid <code>Rc</code> and <code>Arc</code></h3>\n<p>Prefer unique ownership and borrowing over <code>Rc</code> and <code>Arc</code> for managing memory.</p>\n<p>Exception:</p>\n<ul>\n<li>Use <code>Rc</code> and <code>Arc</code> when the alternative ownership and borrowing structure would be unreasonably complex to write, understand, or maintain. This is a judgment call to be discussed during code review.</li>\n</ul>\n<p>Rationale</p>\n<p>Generally, Rust allows representing object graphs without using shared ownership smart pointers. These pointers add runtime overhead and their own unique complexity. Engineers coming from other languages might tend to use <code>Rc</code> and <code>Arc</code> too quickly.</p>\n<h3>Prefer borrows over deep copies</h3>\n<p>Use <code>.clone()</code> when you genuinely need a deep copy of an object.</p>\n<p>Avoid using <code>.clone()</code> as a substitute for borrowing. Design code that works naturally with Rust’s ownership and borrowing rules. However, if the borrowing relationships become excessively complex, <code>.clone()</code> can be a pragmatic choice.</p>\n<p>Consider the performance needs of the code while choosing between cloning and borrowing.</p>\n<p>Note: This advice does not apply to cloning <code>Rc</code> or <code>Arc</code> pointers. Cloning reference-counted pointers does not create a deep copy, and is an expected way to use them.</p>\n<p>Rationale</p>\n<p>Deep copies can be expensive to make due to memory allocation and data copying.</p>\n<h2>Function inputs and outputs</h2>\n<h3>Accept flexible references for immutable data</h3>\n<p>When functions accept borrowed, immutable data, use a reference type that affords the most flexibility. Pass references to the data itself instead of references to smart pointers, containers, or wrappers.</p>\n<table>\n<thead>\n<tr>\n<th>Use</th>\n<th>Instead of</th>\n<th>Why?</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code>&amp;T</code></td>\n<td><code>&amp;Box&lt;T&gt;</code></td>\n<td>Borrow the data inside the box</td>\n</tr>\n<tr>\n<td><code>&amp;[T]</code></td>\n<td><code>&amp;Vec&lt;T&gt;</code></td>\n<td>Accept any slice instead of specific containers</td>\n</tr>\n<tr>\n<td><code>&amp;str</code></td>\n<td><code>&amp;String</code></td>\n<td>Accept a string slice instead of references to owned strings</td>\n</tr>\n<tr>\n<td><code>Option&lt;&amp;T&gt;</code></td>\n<td><code>&amp;Option&lt;T&gt;</code></td>\n<td>Borrow the data inside the Option</td>\n</tr>\n</tbody>\n</table>\n<p>Exceptions:</p>\n<ul>\n<li>When expecting non-linear character data such as Rope/Cord.</li>\n</ul>\n<p>For context, see Clippy lints <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box\"><code>borrowed_box</code></a> and <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg\">ptr_arg</a>.</p>\n<p>Rationale</p>\n<p>Accepting reference types that don’t mention specific smart pointers, containers, or wrappers makes functions more versatile and performant: callers can pass data held in a variety of ways without copying it first.</p>\n<h3>Iterators vs. owned collections in return types</h3>\n<p>Prefer returning iterators (either concrete types or <code>impl Iterator</code>) where possible over returning <code>Vec&lt;T&gt;</code>.</p>\n<p>Note: In application code, returning a <code>Vec&lt;T&gt;</code> is fine if it simplifies the code and if flexibility and performance advantages of iterators are not clearly needed.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Most callers will need to construct an owned vector.</li>\n<li>The function’s implementation naturally constructs a vector.</li>\n<li>The returned value will likely need to be randomly indexed.</li>\n<li>The algorithm works better as a <code>for</code> loop and the forced allocation doesn’t matter too much.</li>\n<li>The function’s intended purpose is creating a new owned collection.</li>\n<li>The function is returning a pre-existing vector.</li>\n</ul>\n<p>Rationale</p>\n<p>Highly reusable library code generally should strive to provide more flexible and performant APIs. Returning an iterator allows callers to choose how to consume the data: collect it into a vector, process it in a loop, or chain further iterator adapters. Some of these options allow users to avoid unnecessary allocations.</p>\n<p>In application code, the usage patterns for a function are often easier to predict. If it is clear that performance is not a concern, or that data would be collected into a vector anyway, returning a vector makes for a simpler API.</p>\n<h3>Iterators vs. slices in function parameters</h3>\n<p>Highly reusable library code should accept iterators or <code>impl IntoIterator</code> if it only needs to iterate over the input.</p>\n<p>Note: In application code, accepting a slice <code>&amp;[T]</code> is fine if it simplifies the code and if flexibility and performance advantages of iterators are not clearly needed.</p>\n<p>Rationale</p>\n<p>Highly reusable library code generally should strive to provide more flexible and performant APIs. Functions that accept iterators offer callers:</p>\n<ul>\n<li>More flexibility (data can be supplied from any collection)</li>\n<li>Efficiency (data does not need to be copied into a contiguous piece of memory)</li>\n<li>Composability (the function works better with iterator chains)</li>\n</ul>\n<p>In application code, the usage patterns for a function are often easier to predict. If it is clear that performance is not a concern, or that callers would always have a vector anyway, accepting a slice makes for a simpler API.</p>\n<h3>Accepting <code>Into</code></h3>\n<p>When designing library APIs that accept a <code>T</code> but users are likely to have various types, consider accepting <code>impl Into&lt;T&gt;</code> instead of <code>T</code> if <strong>call-site ergonomics are paramount</strong>.</p>\n<p>Tip: This should be balanced against code bloat concerns; it is not a requirement to use lots of <code>Into&lt;T&gt;</code> in your code.</p>\n<p>Note: Application code should almost never use <code>impl Into&lt;T&gt;</code> parameters.</p>\n<p>Rationale</p>\n<p>Using <code>impl Into&lt;T&gt;</code> allows a function to directly accept various arguments of various compatible types. For example, a function accepting <code>impl Into&lt;String&gt;</code> could be called with a <code>String</code> or a <code>&amp;str</code> literal directly.</p>\n<p>The tradeoff here is balancing the convenience of avoiding a <code>.into()</code> at the callsite against the cost of generics (more complex API signature, increased code size, sometimes even type inference difficulties).</p>\n<h3>Prefer <code>enum</code> over <code>bool</code> in APIs</h3>\n<p>Use <code>enum</code> function parameters instead of <code>bool</code> in cases where the purpose of the parameter is not obvious.</p>\n<p>If you do use <code>bool</code>, consider using an inline comment to document the purpose of the <code>bool</code> in an argument list.</p>\n<h3>Reporting errors</h3>\n<p>Choose the error reporting mechanism that fits your API contract: <code>Option</code>, <code>Result</code>, or <code>panic!</code>.</p>\n<p><strong>Use <code>Option</code> for simple failures</strong> where the cause is obvious and the caller would not benefit from further details. This is best for standard, anticipated outcomes like a missing value or a parsing mismatch.</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> Good: The cause of the failure is obvious (integer overflow). No further\n</span><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> details could be reasonably provided.\n</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">checked_add</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-parameter syntax-rust\">a</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-storage syntax-type syntax-rust\">u32</span>, <span class=\"syntax-variable syntax-parameter syntax-rust\">b</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-storage syntax-type syntax-rust\">u32</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> <span class=\"syntax-meta syntax-generic syntax-rust\">Option<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span><span class=\"syntax-storage syntax-type syntax-rust\">u32</span><span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span></span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p><strong>Use <code>Result</code> for recoverable failures where the caller needs context</strong> to handle the error. These are often “unavoidable” errors caused by external factors (like I/O or network) that even correctly implemented code cannot prevent with checks ahead of time. Nevertheless, the caller can reasonably anticipate the kinds of errors (a file-related function can fail with an I/O error). The error details allow the caller to distinguish failure modes and recover appropriately.</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> Good: The caller needs to know *why* it failed (NotFound vs PermissionDenied).\n</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">open_file</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-parameter syntax-rust\">path</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span>Path</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> <span class=\"syntax-meta syntax-generic syntax-rust\">Result<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span>File, <span class=\"syntax-meta syntax-path syntax-rust\">io<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Error<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span></span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p><strong>Use <code>panic!</code> to report bugs and violated invariants.</strong> These errors are avoidable: the program is missing a check, and is calling an operation without fulfilling a precondition. These defects must be fixed in the code, not handled at runtime, although coarse-grained recovery might be possible with <code>catch_unwind</code>.</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> Good: Panics if the invariant (index must be in bounds) is violated.\n</span><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> This indicates a bug in the caller&#39;s logic.\n</span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-storage syntax-type syntax-impl syntax-rust\">impl</span></span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-meta syntax-generic syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span>T<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span></span><span class=\"syntax-meta syntax-impl syntax-rust\"> <span class=\"syntax-entity syntax-name syntax-impl syntax-rust\">MyVec</span><span class=\"syntax-meta syntax-generic syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span>T<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span> </span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-modifier syntax-rust\">pub</span> <span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">remove</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> <span class=\"syntax-variable syntax-parameter syntax-rust\">self</span>, <span class=\"syntax-variable syntax-parameter syntax-rust\">index</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-storage syntax-type syntax-rust\">usize</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> T</span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n        <span class=\"syntax-keyword syntax-control syntax-rust\">if</span> index <span class=\"syntax-keyword syntax-operator syntax-rust\">&gt;</span><span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-variable syntax-language syntax-rust\">self</span>.<span class=\"syntax-support syntax-function syntax-rust\">len</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n            <span class=\"syntax-support syntax-macro syntax-rust\">panic!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>index out of bounds<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n        </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n        <span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> ...\n</span>    </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p>Note: Some codebases may choose to avoid panics entirely, and require even broken invariants to be reported via <code>Result</code>.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Developer-facing binaries may use panics for non-fatal errors.</li>\n<li>Tests may use panics for assertions.</li>\n</ul>\n<h2>Types</h2>\n<h3>Newtypes versus type aliases</h3>\n<p>Use a <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/rust-by-example/generics/new_types.html\">newtype</a> instead of a type alias in the following cases:</p>\n<ul>\n<li>If the type you are defining has notable semantic differences.</li>\n<li>For types that warrant an API of their own.</li>\n<li>If you want the flexibility to change the underlying type later, especially if you don’t control all usages of your type.</li>\n<li>For type safety, if it’s easy for the types to get mixed up. For example, a <code>Date</code> type may use newtypes to distinguish <code>Day</code>, <code>Month</code>, and <code>Year</code> numbers in its internal calculations.</li>\n</ul>\n<h3>Prefer named fields over tuples and non-newtype tuple structs</h3>\n<p>Prefer types with named fields over tuples, as tuple indexes (<code>.0</code>, <code>.1</code>, …) quickly become confusing.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Newtype tuple structs (tuple structs with only one field).</li>\n<li>In very local contexts where there is unlikely to be any confusion.</li>\n</ul>\n<h2>Traits</h2>\n<h3>Add trait implementations when they become necessary</h3>\n<p>As a general rule, low-level library code should anticipate users’ needs proactively, while application code should add trait impls as needed.</p>\n<h3>Implement known standard library traits instead of custom methods</h3>\n<p>If a method matches the purpose and behavior of a standard library trait, implement that trait. For example, instead of writing <code>fn from_str(&amp;str) -&gt; Self</code> on <code>Foo</code>, you should <code>impl FromStr for Foo</code>.</p>\n<p>It is acceptable to have both the method and the trait impl for discoverability reasons.</p>\n<p>For context, see Clippy lint <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait\"><code>should_implement_trait</code></a> (<a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://github.com/rust-lang/rust-clippy/blob/928349795f9f28f54e69bd0f0ffeed33de5e272a/clippy_lints/src/methods/mod.rs#L3940-L3971\">list of traits</a>).</p>\n<h3>Use <code>#[derive()]</code> if possible</h3>\n<p>Manual implementations should be avoided if they can be expressed as a <code>#[derive()]</code>.</p>\n<h3>Generics versus <code>dyn Trait</code></h3>\n<p>In libraries, prefer accepting generics (<code>T: Trait</code> or <code>impl Trait</code>) in function parameters over <code>dyn Trait</code>.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Cases where code size matters.</li>\n</ul>\n<p>Rationale</p>\n<ul>\n<li>\n<p>Efficiency</p>\n<ul>\n<li>Additional indirection: Creating a <code>dyn Trait</code> requires setting up a fat pointer, and a vtable lookup is required for every call of a function on the trait.</li>\n<li>If a function takes a <code>dyn Trait</code>, the compiler can’t perform certain optimizations that it could if the function was generic, such as inlining calls to functions of the trait.</li>\n</ul>\n</li>\n<li>\n<p>Some traits are not <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility\"><code>dyn</code> compatible</a>. For example, a trait is not <code>dyn</code> compatible if it has generic functions, associated functions without a <code>self</code> parameter, or functions that return <code>Self</code>.</p>\n</li>\n</ul>\n<h3>When to define a trait versus an inherent implementation</h3>\n<p>Define a trait only for the following purposes:</p>\n<ul>\n<li>\n<p>To enable generic programming (to use <code>T: Trait</code> in constraints of generic functions and types).</p>\n</li>\n<li>\n<p>To accept or return an <code>impl Trait</code> or <code>dyn Trait</code>.</p>\n</li>\n<li>\n<p>To define an <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#extension-traits\">extension trait</a> (but see also the caveats in the section on extension traits).</p>\n</li>\n</ul>\n<p>In all other cases where you want to implement functionality for a type, use an <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/std/keyword.impl.html#inherent-implementations\">inherent implementation</a>.</p>\n<p>Don’t use traits merely to organize code.</p>\n<h3>Extension traits</h3>\n<p>An <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rfcs/0445-extension-trait-conventions.html\">extension trait</a> is a trait that adds methods to a type from a different crate.</p>\n<p>What differentiates an extension trait from other traits is that its sole purpose is to add methods to the type; it is not intended for use in generics (<code>T: Trait</code>, <code>impl Trait</code>) or dynamic dispatch (<code>dyn Trait</code>). To indicate this, the name of an extension trait should carry the suffix <code>~Ext</code> (for example, <code>ForeignTypeExt</code>).</p>\n<p>In general, the team that owns the original type should also own the extension trait.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Extension methods that are expected to become well known and provide significant value, for example, in foundational libraries / frameworks.</li>\n</ul>\n<p>Note: This rule is specifically about extension traits. It is relatively common to define a trait as an input trait (e.g., <code>DateLike</code>) and implement it on a bunch of foreign types.</p>\n<p>Examples</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-other syntax-rust\">extern</span> <span class=\"syntax-keyword syntax-other syntax-rust\">crate</span> foreign_crate<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foreign_crate<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>ForeignType<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n\n<span class=\"syntax-meta syntax-trait syntax-rust\"><span class=\"syntax-storage syntax-type syntax-trait syntax-rust\">trait</span> <span class=\"syntax-entity syntax-name syntax-trait syntax-rust\">ForeignTypeExt</span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">additional_method</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n<span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-storage syntax-type syntax-impl syntax-rust\">impl</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\">ForeignTypeExt <span class=\"syntax-keyword syntax-other syntax-rust\">for</span></span><span class=\"syntax-meta syntax-impl syntax-rust\"> <span class=\"syntax-entity syntax-name syntax-impl syntax-rust\">ForeignType</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">additional_method</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p>It is also possible to define an extension trait for every type that implements some foreign trait. Effectively, this is equivalent to adding default methods to the foreign trait:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-other syntax-rust\">extern</span> <span class=\"syntax-keyword syntax-other syntax-rust\">crate</span> foreign_crate<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-keyword syntax-other syntax-rust\">use</span> <span class=\"syntax-meta syntax-path syntax-rust\">foreign_crate<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>ForeignTrait<span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n\n<span class=\"syntax-meta syntax-trait syntax-rust\"><span class=\"syntax-storage syntax-type syntax-trait syntax-rust\">trait</span> <span class=\"syntax-entity syntax-name syntax-trait syntax-rust\">ForeignTraitExt</span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">additional_method</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n<span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-storage syntax-type syntax-impl syntax-rust\">impl</span></span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-meta syntax-generic syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span>T<span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> ForeignTrait<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span></span><span class=\"syntax-meta syntax-impl syntax-rust\"> ForeignTraitExt <span class=\"syntax-keyword syntax-other syntax-rust\">for</span></span><span class=\"syntax-meta syntax-impl syntax-rust\"> <span class=\"syntax-entity syntax-name syntax-impl syntax-rust\">T</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">additional_method</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p>Rationale</p>\n<p>When the same team owns the extension trait and the type being extended, this helps avoid method name conflicts between the base type and the extension trait or between two extension traits.</p>\n<h3>Specific guidance for important traits</h3>\n<ul>\n<li>\n<p><strong>Types with public fields</strong>: Consider implementing traits implemented by all subfields.</p>\n</li>\n<li>\n<p><strong>Error types</strong>: Implement the <code>Copy</code> trait whenever possible. <code>Copy</code> types are simpler to manage and propagate because they can be passed by value without moving ownership. An example of a case where this is not possible is if the error must contain non-<code>Copy</code> fields like a <code>String</code>.</p>\n</li>\n<li>\n<p><strong><code>Copy</code></strong>: Implement whenever it is reasonably clear that the type will continue to be <code>Copy</code> in the future.</p>\n</li>\n<li>\n<p><strong><code>Clone</code></strong>: Most types should implement <code>Clone</code> unless they represent some external resource.</p>\n</li>\n<li>\n<p><strong><code>Default</code></strong>: Implement if the type implements <code>new()</code> or otherwise has a default state. For context, see Clippy lint <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default\"><code>new_without_default</code></a>.</p>\n</li>\n<li>\n<p><strong><code>PartialEq</code></strong> / <strong><code>Eq</code></strong>: If you want users to use the <code>==</code> operator, prefer implementing <code>PartialEq</code> over <code>Eq</code>. When you do implement <code>Eq</code>, be aware of the additionally required reflexivity (details: <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/std/cmp/trait.Eq.html\">https://doc.rust-lang.org/std/cmp/trait.Eq.html</a>).</p>\n</li>\n<li>\n<p><strong><code>PartialOrd</code></strong> / <strong><code>Ord</code></strong>: If you want users to use the <code>&lt;</code> or <code>&gt;</code> operators, prefer implementing <code>PartialOrd</code> over <code>Ord</code>. When you do implement <code>Ord</code>, be aware that it requires total ordering (details: <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/std/cmp/trait.Ord.html\">https://doc.rust-lang.org/std/cmp/trait.Ord.html</a>).</p>\n</li>\n<li>\n<p><strong>Map keys</strong></p>\n<ul>\n<li>If you think it is likely that a type will be used as a map key or otherwise cached, implement <code>Hash</code> and <code>Eq</code>.</li>\n<li>If, in addition, the type has a clear total ordering, implement <code>Ord</code> (and any traits it needs).</li>\n</ul>\n</li>\n</ul>\n<h3>Pair important <code>From</code> impls with explicit constructor methods</h3>\n<p>If a type has an important <code>From</code> impl that is expected to be one of the primary ways of constructing it, add an inherent <code>::new()</code>, <code>::new_from_foo()</code>¸ or <code>::from_foo()</code> method.</p>\n<h3>Options structs should implement Default</h3>\n<p>Structs with all-public fields that are primarily used as a “bag of options” for a function should implement <code>Default</code>.</p>\n<p>Rationale</p>\n<p>This enables convenient use of struct update syntax (<code>Struct { a: 0, ..Default::default()}</code> ).</p>\n<h2>Concurrency</h2>\n<h3>Handling poison</h3>\n<p>Never reach inside a poisoned mutex to get at its insides, no matter how tempting the use case may seem.</p>\n<h3>Don’t use <code>async</code> / <code>.await</code> [chrome]</h3>\n<p>Don’t use <code>async</code> / <code>.await</code> in Chromium.</p>\n<h3>Don’t start new threads through the Rust standard library [chrome]</h3>\n<p>In Chromium, prefer to use sequences instead of threads.</p>\n<h3>Use <code>fuchsia_sync::Mutex</code> [Fuchsia]</h3>\n<p>Use Fuchsia’s mutex type, rather than one from <code>std::sync</code> or <code>parking_lot</code>.</p>\n<h2>Macros</h2>\n<p>When considering how to express an API, prefer in order:</p>\n<ul>\n<li>\n<p>Regular Rust functions, types, traits, objects, and modules. (Avoid macros, code generation, and metaprogramming where possible).</p>\n</li>\n<li>\n<p><code>macro_rules!</code>-style macros.</p>\n</li>\n<li>\n<p>Other code generation strategies (e.g. <code>genrule</code>)</p>\n</li>\n<li>\n<p><code>proc_macro</code> macros.</p>\n</li>\n</ul>\n<p>When you decide to use a macro or code generation, please weigh the benefits against the costs:</p>\n<ul>\n<li>IDEs, Code Search and other developer tools will work less well on code that uses macros.</li>\n<li>Macros can create dialects of the language that make it harder for engineers to work across the codebase.</li>\n<li>Large scale changes are more difficult and expensive to apply.</li>\n</ul>\n<p>Rationale</p>\n<p><strong>Prefer non-macro facilities:</strong> Macros can create dialects of the language that make it harder for engineers to work across the codebase. When a reader sees a usage of a macro, they can make fewer assumptions about that code than if it didn’t use a macro. Macros are harder to write correctly than regular Rust code.</p>\n<p><strong>Use <code>macro_rules!</code> for simple code generation:</strong> Since Rust macros operate on the AST, they can be less fragile than textual code generation.</p>\n<h2>Naming</h2>\n<h3>Naming methods</h3>\n<p>Follow the Rust standard library style for naming methods. See also the guidance on <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/comprehensive-rust/idiomatic/foundations-api-design/predictable-api/naming-conventions.html\">naming conventions</a> in the Comprehensive Rust course.</p>\n<p>Specific considerations:</p>\n<ul>\n<li>Use prefixes such as <code>as_...()</code>, <code>to_...()</code>, or <code>into_...()</code> with the appropriate <code>self</code> convention. See also <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv\">this section</a> of the Rust API Guidelines and the Clippy lint <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention\"><code>wrong_self_convention</code></a>.</li>\n</ul>\n<h3>Naming lifetimes</h3>\n<p>Lifetimes should have descriptive names, especially type lifetimes.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Type lifetimes that are extremely generic abstractions like <code>Cow</code>.</li>\n<li>It is fine to use anonymous lifetimes (<code>'_</code>) where they work.</li>\n<li>Single-character lifetimes are okay when they are simply expressing the relationship between a method’s input and output, or needed for a local higher-ranked lifetime bounds (<code>for&lt;'a&gt;</code>). Up to two such local lifetimes are fine, if more are needed, we strongly advise you to use descriptive names.</li>\n</ul>\n<h3>Naming generic parameters</h3>\n<p>Use a short name for your generic parameters (either single or multi character), related to the kind of thing they represent. Single-letter generics are okay, but the letter chosen should be somewhat indicative of the object type, ideally taken from the most important trait bound.</p>\n<p><code>T</code> or <code>U</code> are fine when the generics are in a relatively unbound context; i.e. it is an API intended for use with most types, like <code>Vec&lt;T&gt;</code>.</p>\n<h3>Naming extension traits</h3>\n<p>When naming an <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#extension-traits\">extension trait</a>, give the name an <code>~Ext</code> suffix (for example, <code>ForeignTypeExt</code>).</p>\n<h3>Naming initialization functions</h3>\n<p>Follow these guidelines when naming initialization functions:</p>\n<ul>\n<li>\n<p>Constructors: Use <code>new()</code>, or <code>new_with_foo()</code> if there is more than one. If the constructor returns a <code>Result</code>, use <code>try_new()</code>.</p>\n</li>\n<li>\n<p>Value-to-value conversions: Use <code>from_foo()</code> with an associated <code>From</code> impl. See <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/std/convert/trait.From.html#when-to-implement-from\">here</a> for details on when an operation can be considered to be a value-to-value conversion.</p>\n</li>\n</ul>\n<h3>Avoid abbreviations</h3>\n<p>Don’t use abbreviations (especially acronyms and initialisms) that someone outside your project is unlikely to know. Don’t abbreviate by deleting letters within a word.</p>\n<p>See also the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/cppguide.html#General_Naming_Rules\">Google C++ Style Guide</a>.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Common abbreviations found in Wikipedia.</li>\n<li>Abbreviations that are unlikely to be confusing to people outside of your project, for example <code>IpAddr</code>. You and your reviewer should make this determination. In general, the preference should still be not to abbreviate.</li>\n</ul>\n<h2>When and how to write documentation</h2>\n<p>Add documentation comments to public APIs, potentially using the <code>missing_doc</code> lint to help with this.</p>\n<p>Best practices for documentation comments:</p>\n<ul>\n<li>\n<p>Consider including examples: It is not necessary for every function to have examples, but enough functionality should be covered in examples <em>somewhere</em> to give an idea of how the crate works.</p>\n</li>\n<li>\n<p>Always document panic cases for functions which may panic due to malformed input.</p>\n</li>\n<li>\n<p>Clearly document invariants for unsafe functions and traits.</p>\n</li>\n<li>\n<p>Use capitalization and punctuation.</p>\n</li>\n<li>\n<p>Don’t explain the basics of the programming language. For example, don’t do the following:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-struct syntax-rust\"><span class=\"syntax-storage syntax-type syntax-struct syntax-rust\">struct</span> </span><span class=\"syntax-meta syntax-struct syntax-rust\"><span class=\"syntax-entity syntax-name syntax-struct syntax-rust\">MockFileSystem</span> </span><span class=\"syntax-meta syntax-struct syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> ... </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n<span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> A mock implementation of the FileSystem trait, intended to be used in unit tests.\n</span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-storage syntax-type syntax-impl syntax-rust\">impl</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\">FileSystem <span class=\"syntax-keyword syntax-other syntax-rust\">for</span></span><span class=\"syntax-meta syntax-impl syntax-rust\"> <span class=\"syntax-entity syntax-name syntax-impl syntax-rust\">MockFileSystem</span>\n</span></span></span></code></pre>\n</li>\n</ul>\n<p>Exceptions:</p>\n<ul>\n<li>\n<p>Comments should say something that the code does not already say. If there’s nothing insightful to say, it is better to omit the comment than to repeat the function name in a trivial way. For example, don’t do the following (it’s fine to <code>allow</code> the lint in that case):</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Removes an element.\n</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-modifier syntax-rust\">pub</span> <span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">remove_element</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> Element</span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span></span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n</li>\n<li>\n<p>It is generally okay to omit documentation on individual enum variants, especially if their name is self-explanatory.</p>\n</li>\n</ul>\n<p><strong>How to format documentation comments</strong></p>\n<p>Follow these guidelines when writing documentation comments:</p>\n<ul>\n<li>Use <code>///</code> comments to document items following the comment, such as functions, structs, enums, traits, and type aliases.</li>\n<li>Use <code>//!</code> comments to document the module or the library itself. These comments should be placed at the top of the file.</li>\n<li>Follow the rules laid out in <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/nightly/style-guide/#doc-comments\">the official Rust style guide on doc comments</a>.</li>\n<li>Use Markdown syntax.</li>\n<li>Use ordinary comment syntax (<code>//</code>) for non-documentation comments, such as implementation comments, TODOs that are not interesting for users, commented out source code, etc.</li>\n<li>Prefer to document things in a straightforward manner, using <code>///</code> and <code>//!</code> comments, rather than generating documentation from macros. This makes the documentation directly readable when viewing or modifying the source code.</li>\n</ul>\n<p>It is acceptable, and even encouraged, to mix doc comments (<code>///</code> and <code>//!</code>) and ordinary comments (<code>//</code>). If you have one thing you wish to document publicly, and another thing you want to note as an implementation comment, then use both! For example:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Finds the first `FooItem` with the given `name`, or `None` if not present.\n</span><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> TODO: crbug.com/12345 - `name` should be &amp;str, not String.\n</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-modifier syntax-rust\">pub</span> <span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">get_named</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-parameter syntax-rust\">items</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span>[FooItem], <span class=\"syntax-variable syntax-parameter syntax-rust\">name</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> String</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> <span class=\"syntax-meta syntax-generic syntax-rust\">Option<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span>FooItem<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span></span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span><span class=\"syntax-keyword syntax-operator syntax-rust\">...</span></span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p>Rationale</p>\n<p>Many, though not all, languages have a different commenting convention (such as extra <code>*</code> characters) for doc comments as opposed to implementation comments. Rust and Python are notable for not just having different commenting conventions, but an actual different syntax entirely. This has concrete benefits to the Rust ecosystem:</p>\n<ul>\n<li>Doc comments can be consumed by tooling, including documentation generators like <code>rustdoc</code>, IDE features, and Kythe.</li>\n<li>Doc comments have a standard structure and formatting rules (Markdown), allowing for rich text in those contexts.</li>\n<li>Errors in applying doc comment syntax (e.g., applying it to an expression instead of a documentable item) are linted against, catching documentation errors early. If there are no lint errors, the doc comments are guaranteed to be visible to documentation tooling, removing a degree of flakiness and unpredictability from the documentation process.</li>\n<li>Doc comments are a part of the AST, and so can be produced and consumed by macros, including both <code>macro_rules!</code> macros and procedural macros.</li>\n</ul>\n<h2>Other Rust features</h2>\n<h3>Unsafe code</h3>\n<p>Don’t write unsafe code unless there is no way to implement the same functionality with safe code.</p>\n<p>If you do have to write unsafe code:</p>\n<ul>\n<li>Encapsulate unsafe code in a safe abstraction and isolate it from business logic as much as possible. Ideally, unsafe code should be in a separate crate with few dependencies beyond the standard library.</li>\n<li>Add a safety comment starting with <code>SAFETY:</code> that explains why the code is correct.</li>\n<li>Add a technical justification for unsafe code. Depending on context, this can be a justification for an individual unsafe block or a more general justification that covers an entire file or build target. The justification should answer these questions:\n<ul>\n<li>Why is this code not written in an equivalent safe way?</li>\n<li>If the reason is performance, are there benchmarks?</li>\n</ul>\n</li>\n</ul>\n<h3>No unstable features</h3>\n<p>Don’t use unstable features.</p>\n<p>Exceptions:</p>\n<ul>\n<li>Android Embedded programming may require the use of certain unstable features. While review will be required to enable them (to help search for alternatives), features absolutely required to build an artifact correctly will generally be approved.</li>\n<li>Chrome Style arbiters may blanket-approve a small list of features for the entire codebase. Typically, only features that have a clear path to stabilization will be approved.</li>\n<li>Fuchsia See guidance <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://fuchsia.dev/fuchsia-src/development/languages/rust/unstable\">here</a>.</li>\n</ul>\n<h3>Getters and setters</h3>\n<p>Writing and naming getters and setters should follow these guidelines:</p>\n<ul>\n<li><code>foo()</code> for a getter that returns a <code>T</code> by value.</li>\n<li><code>foo_mut()</code> for a getter that returns a <code>&amp;mut T</code>.</li>\n<li><code>take_foo()</code> for a getter that “clears” the value (by setting it to <code>None</code> or some other default state) and returns the old value.</li>\n<li><code>set_foo()</code> for a setter that takes a <code>T</code> by value.</li>\n<li><code>with_foo()</code> / <code>with_mut_foo()</code> for an accessor that operates via a closure (see details <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#closure-accessors\">below</a>). Such an accessor is invoked as <code>object.with_foo(|foo| frob(foo))</code>.</li>\n<li><code>replace_foo()</code> for a setter that returns the old value.</li>\n</ul>\n<p>Examples</p>\n<pre><code class=\"language-rs\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-storage syntax-type syntax-impl syntax-rust\">impl</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-entity syntax-name syntax-impl syntax-rust\">Stuff</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n   <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">foo</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span>Foo</span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span>  </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n   <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">foo_mut</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> <span class=\"syntax-variable syntax-parameter syntax-rust\">self</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> Foo</span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n   <span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Sets &quot;baz&quot; property to `None` and returns the old value.\n</span>   <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">take_baz</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> <span class=\"syntax-variable syntax-parameter syntax-rust\">self</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> <span class=\"syntax-meta syntax-generic syntax-rust\">Option<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span>Baz<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span></span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n   <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">set_foo</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> <span class=\"syntax-variable syntax-parameter syntax-rust\">self</span>, <span class=\"syntax-variable syntax-parameter syntax-rust\">foo</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> Foo</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n   <span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Calls `f` with a borrow of the &quot;bar&quot; property.\n</span>   <span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Returns the return value of `f`.\n</span>   <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">with_bar</span></span><span class=\"syntax-meta syntax-generic syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span>R<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span>, <span class=\"syntax-variable syntax-parameter syntax-rust\">f</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> impl FnOnce<span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\">&amp;<span class=\"syntax-variable syntax-parameter syntax-rust\">Bar</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> -&gt; R</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> R</span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n   <span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Calls `f` with a mutable borrow of the &quot;bar&quot; property.\n</span>   <span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Returns the return value of `f`.\n</span>   <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">with_mut_bar</span></span><span class=\"syntax-meta syntax-generic syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span>R<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span>, <span class=\"syntax-variable syntax-parameter syntax-rust\">f</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> impl FnOnce<span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\">&amp;<span class=\"syntax-variable syntax-parameter syntax-rust\">mut</span> <span class=\"syntax-variable syntax-parameter syntax-rust\">i32</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> -&gt; R</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> R</span> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n   <span class=\"syntax-comment syntax-line syntax-documentation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">///</span> Sets &quot;foo&quot; property to `foo` and returns the old value.\n</span>   <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">replace_foo</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> <span class=\"syntax-variable syntax-parameter syntax-rust\">self</span>, <span class=\"syntax-variable syntax-parameter syntax-rust\">foo</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> Foo</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> <span class=\"syntax-meta syntax-function syntax-return-type syntax-rust\"><span class=\"syntax-punctuation syntax-separator syntax-rust\">-&gt;</span> Foo</span>  </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span> </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<h4>More on accessors that accept closures</h4>\n<p>Accessors that accept a closure (<code>.with_foo()</code>) are typically invoked as <code>object.with_foo(|foo| do_something_with(foo))</code>. They can be a good choice in the following situations:</p>\n<ul>\n<li>\n<p>When the property is contained in an interior mutability wrapper (e.g. <code>Mutex</code> or <code>RefCell</code>). These mechanisms need to constrain access to the property to a scope, and the closure provides this scope. This is usually preferable to directly returning the RAII object that implements the scope (e.g. <code>MutexGuard&lt;T&gt;</code>) because the latter would leak an implementation detail.</p>\n</li>\n<li>\n<p>When follow-up work or validation is needed after a property is changed and a <code>set_foo()</code> accessor cannot be used (for example because the property’s type is expensive to clone or cannot be cloned at all).</p>\n</li>\n</ul>\n<p>A <code>with_foo()</code> accessor will often allow the closure to return a value and forward it by returning it from <code>with_foo()</code>.</p>\n<p>If you need to provide two closure-accepting accessors, one for read-only access and one for mutable access, it is common to name them <code>with_foo()</code> and <code>with_mut_foo()</code>. Otherwise, <code>with_foo()</code> is often also used for mutating accessors.</p>\n<h3>Implement expected API sets together</h3>\n<p>If you implement <code>len()</code>, also implement <code>is_empty()</code>. For context, see Clippy lint <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty\"><code>len_without_is_empty</code></a>.</p>\n<h3>Do not access items marked <code>#[doc(hidden)]</code> that you don’t own</h3>\n<p>Treat items marked <code>#[doc(hidden)]</code> as private. Don’t access them unless you own the code.</p>\n<h3>Use <code>const</code> functions responsibly</h3>\n<p>Mark a public function as <code>const</code> only if you expect it to remain <code>const</code> in the future (or can commit to helping clients migrate when it changes).</p>\n<p>Rationale</p>\n<p>It’s a breaking change to turn a <code>const</code> function into a regular function.</p>\n<h3>Avoid overloading for non-mathematical purposes</h3>\n<p>Use operator overloads for their original mathematical/logical purpose, not just to make the code more concise (as “syntax sugar”).</p>\n<h3>Apply <code>#[non_exhaustive]</code> to publicly-exposed enums</h3>\n<p>If an enum is part of a public API used by other teams, mark it <code>#[non_exhaustive]</code>.</p>\n<p>Exceptions:</p>\n<ul>\n<li>The enum will never change. For example:\n<ul>\n<li>Enums describing an existing ontology (e.g., <code>enum Color { Red, Green, Blue }</code>).</li>\n<li>Enums with two states that could be Booleans (e.g., <code>enum Visibility { Visible, Hidden }</code>)</li>\n</ul>\n</li>\n<li>The enum <em>may</em> change in the future, but you are committed to fixing resulting breakages via an LSC.</li>\n</ul>\n<p>For context, see Clippy lint <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_enums\"><code>exhaustive_enums</code></a>.</p>\n<p>Rationale</p>\n<p>Marking an enum <code>#[non_exhaustive]</code> forces users to include a wildcard arm (<code>_ =&gt; ...</code>) in <code>match</code> statements. This ensures code continues to compile when new variants are added to the enum later.</p>\n<p>This rule is primarily important for enums used across team boundaries, because the scope of the breakage could be significant. Enums used only a small number of times can’t create a large breakage and thus may ignore this rule.</p>\n<h3>Apply <code>#[non_exhaustive]</code> to public structs with public fields</h3>\n<p>Structs with public fields that are part of a public API used by other teams should be marked <code>#[non_exhaustive]</code>.</p>\n<p>Exceptions:</p>\n<ul>\n<li>The struct fields are unlikely to change, e.g., because they describe an existing ontology (e.g., a Rectangle type) or the reviewer can otherwise be confident that they will not change.</li>\n<li>The struct fields <em>may</em> change in the future, but you are committed to fixing resulting breakages via an LSC.</li>\n<li>You want to enable your users to use functional record update (FRU) struct initialization syntax (<code>Person { name: &quot;foo&quot;, ..Default::default() }</code>).</li>\n</ul>\n<p>Rationale</p>\n<p>Adding a new field (even a private one) to a struct with all public fields is a breaking change, because callers can no longer name all fields in a struct literal.</p>\n<p>A <code>#[non_exhaustive]</code> struct can’t be constructed with the struct literal syntax, except for the code in the same crate. This allows new private fields to be added without breaking users.</p>\n<p>This rule is primarily important for structs used across team boundaries, because the scope of the breakage could be significant. Structs used only a small number of times can’t create a large breakage and thus may ignore this rule.</p>\n<h3><code>#[must_use]</code> on functions</h3>\n<p>Use <code>#[must_use]</code> on functions in these cases:</p>\n<ul>\n<li>\n<p>The caller cannot use the function correctly if they ignore the return value. This includes pure functions (functions without side effects), such as <code>HashMap::get()</code>.</p>\n</li>\n<li>\n<p>Any function whose return value represents an error or otherwise critical information about the function’s behavior. If the return type is already annotated <code>#[must_use]</code> (which is true of <code>Result</code>, for example), the function does not need to be annotated <code>#[must_use]</code>.</p>\n</li>\n</ul>\n<h3><code>#[must_use]</code> on types</h3>\n<p>Use <code>#[must_use]</code> on types intended to be returned by functions that would otherwise be annotated <code>#[must_use]</code> according to the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://google.github.io/styleguide/rustguide.html#must-use-on-functions\">rules above</a>.</p>\n<p>Examples:</p>\n<ul>\n<li>\n<p>The type is an error type (similar to <code>Result</code> or <code>StatusOr</code>).</p>\n</li>\n<li>\n<p>The type represents a lazily evaluated computation. In other words, the function call doesn’t do anything by itself, and the return value needs to be accessed to cause some desired side effect.</p>\n</li>\n</ul>\n<h3>Do not run code before <code>main()</code></h3>\n<p>Do not cause code to run before <code>main()</code>, for example by using techniques such as the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://docs.rs/ctor/latest/ctor/\">ctor crate</a>.</p>\n<h2>Implementation concerns</h2>\n<h3>Don’t overuse type inference</h3>\n<p>Add enough explicit types so that someone familiar with the codebase can figure out what the inferred types are.</p>\n<p>Prefer to add the types to variable declarations, rather than as <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://doc.rust-lang.org/reference/glossary.html#turbofish\">turbofishes</a>. If the compiler forces you to add a turbofish, it is generally fine to add a <code>_</code> inference marker.</p>\n<p>Specific situations in which you should probably add types:</p>\n<ul>\n<li>When calling methods with a generic return type, such as <code>.into()</code> or <code>.parse()</code>.</li>\n<li>For the result of complex iterator chains.</li>\n</ul>\n<h3>Prefer pattern matching over check-then-unwrap</h3>\n<p>Avoid potentially panicking operations when the input data guarantees they cannot panic. For example, instead of using <code>Option::unwrap</code> on a value known to be valid, use pattern matching to prove correctness to the compiler.</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> Good: The match arm proves to the compiler that `x` is `Some`.\n</span><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> A panic is obviously not possible.\n</span><span class=\"syntax-keyword syntax-control syntax-rust\">if</span> <span class=\"syntax-storage syntax-type syntax-rust\">let</span> <span class=\"syntax-support syntax-type syntax-rust\">Some</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>value</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> option <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-support syntax-function syntax-rust\">use_value</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>value</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span></span></code></pre>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> Bad: Correctness of `unwrap` relies on a runtime check that `option` is `Some`.\n</span><span class=\"syntax-keyword syntax-control syntax-rust\">if</span> option.<span class=\"syntax-support syntax-function syntax-rust\">is_some</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-support syntax-function syntax-rust\">use_value</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>option.<span class=\"syntax-support syntax-function syntax-rust\">unwrap</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span></span></code></pre>\n<h3>Prefer <code>expect</code> over <code>unwrap</code></h3>\n<p>If a panic is necessary because an invariant cannot be expressed in the type system, prefer <code>expect</code> over <code>unwrap</code> to document the reason.</p>\n<h3>Don’t omit patterns on <code>non_exhaustive</code> enums when matching</h3>\n<p>Prefer to list all enum variants in <code>match</code> statements over <code>non_exhaustive</code> enums.</p>\n<p>If you don’t care about specific variants, place them in the wildcard arm. If you <em>only</em> care about specific variants, use an <code>if let</code> or a <code>let … else {}</code>.</p>\n<p>Examples:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-annotation syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-annotation syntax-rust\">#</span><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">[</span><span class=\"syntax-variable syntax-annotation syntax-rust\">non_exhaustive</span><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">]</span></span>\n<span class=\"syntax-meta syntax-enum syntax-rust\"><span class=\"syntax-storage syntax-modifier syntax-rust\">pub</span> <span class=\"syntax-storage syntax-type syntax-enum syntax-rust\">enum</span> <span class=\"syntax-entity syntax-name syntax-enum syntax-rust\">Shape</span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    Circle<span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n    Square<span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n    Triangle<span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">process_shape</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-parameter syntax-rust\">shape</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> Shape</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-keyword syntax-control syntax-rust\">match</span> shape <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-meta syntax-path syntax-rust\">Shape<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Circle <span class=\"syntax-keyword syntax-operator syntax-rust\">=&gt;</span> <span class=\"syntax-support syntax-macro syntax-rust\">println!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>It&#39;s a circle<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n    <span class=\"syntax-meta syntax-path syntax-rust\">Shape<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Square <span class=\"syntax-keyword syntax-operator syntax-rust\">=&gt;</span> <span class=\"syntax-support syntax-macro syntax-rust\">println!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>It&#39;s a square<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n    <span class=\"syntax-comment syntax-line syntax-double-slash syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-comment syntax-rust\">//</span> Triangle and any future variants don&#39;t require any actions.\n</span>    <span class=\"syntax-meta syntax-path syntax-rust\">Shape<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Triangle <span class=\"syntax-keyword syntax-operator syntax-rust\">|</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">_</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">=&gt;</span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span></span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n  </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">process_shape</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-parameter syntax-rust\">shape</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> Shape</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-keyword syntax-control syntax-rust\">if</span> <span class=\"syntax-storage syntax-type syntax-rust\">let</span> <span class=\"syntax-meta syntax-path syntax-rust\">Shape<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Circle <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> shape <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-support syntax-macro syntax-rust\">println!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>It&#39;s a circle<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n  </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">process_shape</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-parameter syntax-rust\">shape</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> Shape</span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-keyword syntax-control syntax-rust\">match</span> shape <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-meta syntax-path syntax-rust\">Shape<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>Circle <span class=\"syntax-keyword syntax-operator syntax-rust\">=&gt;</span> <span class=\"syntax-support syntax-macro syntax-rust\">println!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>It&#39;s a circle<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n    <span class=\"syntax-keyword syntax-operator syntax-rust\">_</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">=&gt;</span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span></span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n  </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p>Exceptions:</p>\n<ul>\n<li>You only care about specific variants <em>and</em> <code>match</code> is cleaner than <code>if let</code>.</li>\n</ul>\n<p>This is covered by the built-in lint <code>non_exhaustive_omitted_patterns</code> which you can enable on your code.</p>\n<h3>Use context strings to clarify assertions</h3>\n<p>While some assertions are self-explanatory, many benefit from additional context that clarifies why an assertion is expected to hold, or what caused it to be violated. Use the optional format string argument for <code>assert!</code> to convey this context.</p>\n<p>You should always consider adding context, but particularly in the following situations:</p>\n<ul>\n<li>If you’re thinking of adding a comment to the assertion. Use a format string instead: It has the benefit that it will appear in logs.</li>\n<li>If you expect failing assertions to be seen by users. In this case, a human-readable explanation is more helpful than a Rust expression.</li>\n<li>If logging a value helps in understanding why the assertion failed.</li>\n<li>To clarify whether a failing assertion means that\n<ul>\n<li>The client misused an API (i.e., they should change their use of the API), or</li>\n<li>The implementation contains a bug (i.e., the client should file a bug report).</li>\n</ul>\n</li>\n</ul>\n<p>Examples</p>\n<p><strong>Explaining why an assertion should hold</strong></p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\">    <span class=\"syntax-support syntax-macro syntax-rust\">assert!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>\n        values.<span class=\"syntax-support syntax-function syntax-rust\">is_sorted</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n        <span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>`values` is populated from a `PriorityQueue`, so we expect it to be sorted<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span>\n    </span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<p><strong>Logging a value to clarify why the assertion failed</strong></p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-support syntax-macro syntax-rust\">assert!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>refund <span class=\"syntax-keyword syntax-operator syntax-rust\">&gt;</span> <span class=\"syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust\">0</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span> <span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>Refund must be positive, got {refund:?});\n</span></span></span></span></code></pre>\n<p><strong>Flagging misuse of an API</strong></p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-storage syntax-type syntax-impl syntax-rust\">impl</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-entity syntax-name syntax-impl syntax-rust\">Database</span> </span><span class=\"syntax-meta syntax-impl syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span>\n\n    <span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-storage syntax-type syntax-function syntax-rust\">fn</span> </span><span class=\"syntax-entity syntax-name syntax-function syntax-rust\">execute_query</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">self</span>, <span class=\"syntax-variable syntax-parameter syntax-rust\">query</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-storage syntax-type syntax-rust\">str</span></span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-function syntax-rust\"> </span><span class=\"syntax-meta syntax-function syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n        <span class=\"syntax-support syntax-macro syntax-rust\">assert!</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-variable syntax-language syntax-rust\">self</span>.stream.<span class=\"syntax-support syntax-function syntax-rust\">is_some</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span> <span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>Call connect() before executing queries<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n    </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n\n    <span class=\"syntax-keyword syntax-operator syntax-rust\">...</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span>\n</span></span></code></pre>\n<p><strong>Don’t use the context string merely to repeat the condition</strong></p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-support syntax-function syntax-rust\">assert</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">!</span>values.<span class=\"syntax-support syntax-function syntax-rust\">is_sorted</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span> <span class=\"syntax-string syntax-quoted syntax-double syntax-rust\"><span class=\"syntax-punctuation syntax-definition syntax-string syntax-begin syntax-rust\">&quot;</span>values should always be sorted<span class=\"syntax-punctuation syntax-definition syntax-string syntax-end syntax-rust\">&quot;</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<h3>Don’t use iterator combinator chains when direct iteration is clearer</h3>\n<p>Avoid using combinator chains in ways that hurt readability. Specifically:</p>\n<ul>\n<li>Avoid long and complex combinator chains.</li>\n<li>Don’t construct combinator chains in the header of a <code>for</code> loop. Instead, assign the combinator chain to a variable, then consume that variable in the <code>for</code> loop.</li>\n<li>Avoid <code>Iterator::for_each</code> unless you’re convinced you have a good reason. A <code>for</code> loop is usually easier to read.</li>\n</ul>\n<p>Combinators <em>are</em> preferable for shorter chains and for cases where the combinator itself provides a lot of logic that would otherwise have to be written out in the corresponding <code>for</code> loop.</p>\n<p>Examples</p>\n<p><strong>Complex combinator chains</strong></p>\n<p>Here’s an example of a combinator chain that is hard to read because it involves conditionals and mutates external state:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-storage syntax-type syntax-rust\">let</span> <span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> error_count <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust\">0</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-storage syntax-type syntax-rust\">let</span> _processed_items<span class=\"syntax-punctuation syntax-separator syntax-rust\">:</span> <span class=\"syntax-meta syntax-generic syntax-rust\">Vec<span class=\"syntax-punctuation syntax-definition syntax-generic syntax-begin syntax-rust\">&lt;</span><span class=\"syntax-keyword syntax-operator syntax-rust\">_</span><span class=\"syntax-punctuation syntax-definition syntax-generic syntax-end syntax-rust\">&gt;</span></span> <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> items\n    .<span class=\"syntax-support syntax-function syntax-rust\">into_iter</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">map</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>transformation_that_may_fail</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">map</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">|</span></span></span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-variable syntax-parameter syntax-rust\">result</span><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">|</span></span> </span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n        <span class=\"syntax-keyword syntax-control syntax-rust\">if</span> result.<span class=\"syntax-support syntax-function syntax-rust\">is_err</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n            error_count <span class=\"syntax-keyword syntax-operator syntax-rust\">+</span><span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust\">1</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n        </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n        result\n    </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">filter_map</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-support syntax-type syntax-rust\">Result</span><span class=\"syntax-meta syntax-path syntax-rust\"><span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>ok</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">collect</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<p>The corresponding <code>for</code> loop is easier to read:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-storage syntax-type syntax-rust\">let</span> <span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> error_count <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust\">0</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-storage syntax-type syntax-rust\">let</span> <span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> processed_items <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-support syntax-type syntax-rust\">Vec</span><span class=\"syntax-meta syntax-path syntax-rust\"><span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span>with_capacity<span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>items.<span class=\"syntax-support syntax-function syntax-rust\">len</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n\n<span class=\"syntax-keyword syntax-control syntax-rust\">for</span> item <span class=\"syntax-keyword syntax-operator syntax-rust\">in</span> items <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-keyword syntax-control syntax-rust\">match</span> <span class=\"syntax-support syntax-function syntax-rust\">transformation_that_may_fail</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>item</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n        <span class=\"syntax-support syntax-type syntax-rust\">Ok</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>val</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> <span class=\"syntax-keyword syntax-operator syntax-rust\">=&gt;</span> processed_items.<span class=\"syntax-support syntax-function syntax-rust\">push</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>val</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n        <span class=\"syntax-support syntax-type syntax-rust\">Err</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-keyword syntax-operator syntax-rust\">_</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span> <span class=\"syntax-keyword syntax-operator syntax-rust\">=&gt;</span> error_count <span class=\"syntax-keyword syntax-operator syntax-rust\">+</span><span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-constant syntax-numeric syntax-integer syntax-decimal syntax-rust\">1</span><span class=\"syntax-punctuation syntax-separator syntax-rust\">,</span>\n    </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span></span></code></pre>\n<p><strong>Combinator chains in for loops</strong></p>\n<p>Avoid constructing a combinator chain in the header of a <code>for</code> loop:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-keyword syntax-control syntax-rust\">for</span> bug <span class=\"syntax-keyword syntax-operator syntax-rust\">in</span> bug_descriptions\n    .<span class=\"syntax-support syntax-function syntax-rust\">iter</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">filter_map</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">|</span></span></span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-variable syntax-parameter syntax-rust\">description</span><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">|</span></span> </span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-support syntax-function syntax-rust\">parse_bug_description</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>description</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>.<span class=\"syntax-support syntax-function syntax-rust\">ok</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">filter</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">|</span></span></span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-variable syntax-parameter syntax-rust\">bug</span><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">|</span></span> </span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\">bug.priority <span class=\"syntax-keyword syntax-operator syntax-rust\">&lt;</span><span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-meta syntax-path syntax-rust\">Priority<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-constant syntax-other syntax-rust\">P1</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n<span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-support syntax-function syntax-rust\">assign_bug</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>bug</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span></span></code></pre>\n<p>Instead, assign the combinator chain to a variable. This reduces clutter, and the variable name documents what the combinator chain does:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-storage syntax-type syntax-rust\">let</span> urgent_bugs <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> bug_descriptions\n    .<span class=\"syntax-support syntax-function syntax-rust\">iter</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">filter_map</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">|</span></span></span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-variable syntax-parameter syntax-rust\">description</span><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">|</span></span> </span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-support syntax-function syntax-rust\">parse_bug_description</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>description</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>.<span class=\"syntax-support syntax-function syntax-rust\">ok</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>\n    .<span class=\"syntax-support syntax-function syntax-rust\">filter</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">|</span></span></span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-variable syntax-parameter syntax-rust\">bug</span><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">|</span></span> </span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\">bug.priority <span class=\"syntax-keyword syntax-operator syntax-rust\">&lt;</span><span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-meta syntax-path syntax-rust\">Priority<span class=\"syntax-punctuation syntax-accessor syntax-rust\">::</span></span><span class=\"syntax-constant syntax-other syntax-rust\">P1</span></span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n\n<span class=\"syntax-keyword syntax-control syntax-rust\">for</span> bug <span class=\"syntax-keyword syntax-operator syntax-rust\">in</span> urgent_bugs <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    <span class=\"syntax-support syntax-function syntax-rust\">assign_bug</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span>bug</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span></span></code></pre>\n<p><strong>When to prefer a combinator chain</strong></p>\n<p>Here’s an example where a combinator chain is preferable:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-storage syntax-type syntax-rust\">let</span> threshold_exceeded <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> values.<span class=\"syntax-support syntax-function syntax-rust\">iter</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span>.<span class=\"syntax-support syntax-function syntax-rust\">any</span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-begin syntax-rust\">(</span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-begin syntax-rust\">|</span></span></span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\"><span class=\"syntax-meta syntax-function syntax-parameters syntax-rust\"><span class=\"syntax-keyword syntax-operator syntax-rust\">&amp;</span><span class=\"syntax-variable syntax-parameter syntax-rust\">v</span><span class=\"syntax-punctuation syntax-section syntax-parameters syntax-end syntax-rust\">|</span></span> </span><span class=\"syntax-meta syntax-function syntax-closure syntax-rust\">v <span class=\"syntax-keyword syntax-operator syntax-rust\">&gt;</span> threshold</span></span><span class=\"syntax-meta syntax-group syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-group syntax-end syntax-rust\">)</span></span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n</span></span></code></pre>\n<p>The corresponding for loop is harder to read and reinvents the logic contained in <code>any()</code>:</p>\n<pre><code class=\"language-rust\"><span class=\"code-snippet\" data-language=\"Rust\"><span class=\"syntax-source syntax-rust\"><span class=\"syntax-storage syntax-type syntax-rust\">let</span> <span class=\"syntax-storage syntax-modifier syntax-rust\">mut</span> threshold_exceeded <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-constant syntax-language syntax-rust\">false</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n<span class=\"syntax-keyword syntax-control syntax-rust\">for</span> v <span class=\"syntax-keyword syntax-operator syntax-rust\">in</span> values <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n  <span class=\"syntax-keyword syntax-control syntax-rust\">if</span> v <span class=\"syntax-keyword syntax-operator syntax-rust\">&gt;</span> threshold <span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-begin syntax-rust\">{</span>\n    threshold_exceeded <span class=\"syntax-keyword syntax-operator syntax-rust\">=</span> <span class=\"syntax-constant syntax-language syntax-rust\">true</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n    <span class=\"syntax-keyword syntax-control syntax-rust\">break</span><span class=\"syntax-punctuation syntax-terminator syntax-rust\">;</span>\n  </span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span><span class=\"syntax-meta syntax-block syntax-rust\"><span class=\"syntax-punctuation syntax-section syntax-block syntax-end syntax-rust\">}</span></span>\n</span></span></code></pre>\n<h3>Don’t call <code>panic::catch_unwind</code></h3>\n<p>Use of <code>panic::catch_unwind</code> is prohibited in first-party Rust code.</p>\n<p>Exceptions:</p>\n<ul>\n<li>It is acceptable as a defense in depth mechanism as long as it is never <em>relied</em> upon for any properties. A codebase that is already attempting to avoid panics may choose to wrap critical entry points with a <code>catch_unwind</code> recovery mechanism just in case a panic slips through. Code authors must convince the reviewer that the codebase is doing most of what it can to avoid panics. This exception only applies to code that has the possibility of being run with <code>panic=unwind</code> (so this does not apply to Chrome, Android, Fuchsia).</li>\n</ul>\n<p>Rationale</p>\n<p>Rust code may be used in many environments, and <code>panic::catch_unwind</code> will only function correctly in environments with <code>panic=unwind</code>. While the final panic disposition of prod binaries has not yet been determined, Chrome, Android, and Fuchsia all require <code>panic=abort</code>.</p>\n<h3>Prefer <code>format!</code> / <code>write!</code> for concatenation and substitution</h3>\n<p>Prefer <code>format!()</code> and <code>write!()</code> to other alternatives in the following situations:</p>\n<ul>\n<li>To concatenate more than two strings. For example, use <code>format!(&quot;{a}{b}{c}&quot;)</code> instead of <code>a + b + c</code>.</li>\n<li>To perform substitutions on a literal string (instead of using <code>str::replace</code>).</li>\n</ul>\n<p>Rationale</p>\n<ul>\n<li><code>format!</code> / <code>write!</code> require fewer allocations than concatenating strings using <code>+</code>.</li>\n<li>Prefer standard format strings to inventing your own templating syntax.</li>\n</ul>\n<h2>Use the right per-codebase test framework</h2>\n<p>Most codebases use their own specific test framework rather than <code>#[test]</code>.</p>\n<ul>\n<li>\n<p>Chrome Use <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://source.chromium.org/chromium/chromium/src/+/main:testing/rust_gtest_interop/\">rust_gtest_interop</a>. This makes it possible to integrate Rust tests into C++ test binaries.</p>\n</li>\n<li>\n<p>Fuchsia Use <code>#[fuchsia::test]</code>. See <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://fuchsia.dev/fuchsia-src/development/languages/rust/testing.md\">here</a> for more details.</p>\n</li>\n</ul>\n",
      "date_modified": "2026-10-21T19:11:57+00:00",
      "date_published": "2026-10-21T19:11:57+00:00",
      "external_url": "https://google.github.io/styleguide/rustguide.html",
      "id": "urn:aggr:item:7fbf0c3c9bff2fa6b569da668ceac4b5c8df010e",
      "summary": "Rust Style Guide Background Scope Goals of the style guide Follow public Rust style rules Apply API design rules in context Code organization Prefer small crates [chrome] Use flat file layout for rust_library sources [chrome] Name source…",
      "tags": [
        "news"
      ],
      "title": "Google's Rust Style Guide",
      "url": "https://aggr.aymericbeaumet.com/items/hnrss-org/2026-10-21-google-s-rust-style-guide/"
    }
  ],
  "language": "en",
  "title": "google.github.io",
  "version": "https://jsonfeed.org/version/1.1"
}