docs: add the rust best practices skill
Built from apollographql/rust-best-practices (Apache-2.0): its rules file as SKILL.md and the handbook chapters as references. Used for the API and the website server.
This commit is contained in:
10
CLAUDE.md
10
CLAUDE.md
@@ -1,12 +1,13 @@
|
|||||||
# Style guide
|
# Style guide
|
||||||
|
|
||||||
This repo holds the skills that set how code, comments and commits look in the Firka projects. When you work here, or are asked to set up a machine, install these three things.
|
This repo holds the skills that set how code, comments and commits look in the Firka projects. When you work here, or are asked to set up a machine, install these four things.
|
||||||
|
|
||||||
| What | Where it comes from | What it does |
|
| What | Where it comes from | What it does |
|
||||||
|------|---------------------|--------------|
|
|------|---------------------|--------------|
|
||||||
| `firka-style` | `skills/firka-style/` | style guide for the Firka mobile app: code, comments, commits, new screens, branching and PRs |
|
| `firka-style` | `skills/firka-style/` | style guide for the Firka mobile app: code, comments, commits, new screens, branching and PRs |
|
||||||
| `anti-over-engineering` | `skills/anti-over-engineering/` | keep changes small, reuse what exists, no new abstractions |
|
| `anti-over-engineering` | `skills/anti-over-engineering/` | keep changes small, reuse what exists, no new abstractions |
|
||||||
| `graphify` | `skills/graphify/` (original, unchanged) plus the PyPI package `graphifyy` | knowledge graph of a codebase, query it instead of reading every file |
|
| `graphify` | `skills/graphify/` (original, unchanged) plus the PyPI package `graphifyy` | knowledge graph of a codebase, query it instead of reading every file |
|
||||||
|
| `rust-best-practices` | `skills/rust-best-practices/` (from Apollo's handbook) | how to write and review Rust: ownership, errors, clippy, tests, docs |
|
||||||
|
|
||||||
`skills/firka-style-website/` is the same kind of guide for the website. Install it too if you work on the website.
|
`skills/firka-style-website/` is the same kind of guide for the website. Install it too if you work on the website.
|
||||||
|
|
||||||
@@ -17,7 +18,8 @@ Each Firka repo has a short `CLAUDE.md` that sends you here. Find the repo you c
|
|||||||
| Repo | Style skill |
|
| Repo | Style skill |
|
||||||
|------|-------------|
|
|------|-------------|
|
||||||
| `firka/firka` (phone and Wear OS app) | `skills/firka-style/SKILL.md` |
|
| `firka/firka` (phone and Wear OS app) | `skills/firka-style/SKILL.md` |
|
||||||
| `firka/website` | `skills/firka-style-website/SKILL.md` |
|
| `firka/website` | `skills/firka-style-website/SKILL.md`, plus `rust-best-practices` for `server/` |
|
||||||
|
| `firka/api-v2` (the API) | `skills/firka-style-website/SKILL.md` for comments and commits, plus `rust-best-practices` |
|
||||||
|
|
||||||
`anti-over-engineering` and `graphify` apply to every repo. Read the style skill from this repo directly. Installing only matters for `/graphify` and for later sessions, which pick skills up on start.
|
`anti-over-engineering` and `graphify` apply to every repo. Read the style skill from this repo directly. Installing only matters for `/graphify` and for later sessions, which pick skills up on start.
|
||||||
|
|
||||||
@@ -27,7 +29,7 @@ Check what is already there first, then install only what is missing. Run from t
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
mkdir -p ~/.claude/skills
|
mkdir -p ~/.claude/skills
|
||||||
cp -r skills/firka-style skills/anti-over-engineering skills/graphify ~/.claude/skills/
|
cp -r skills/firka-style skills/anti-over-engineering skills/graphify skills/rust-best-practices ~/.claude/skills/
|
||||||
# website work only:
|
# website work only:
|
||||||
cp -r skills/firka-style-website ~/.claude/skills/
|
cp -r skills/firka-style-website ~/.claude/skills/
|
||||||
|
|
||||||
@@ -45,7 +47,7 @@ graphify install
|
|||||||
## Working in this repo
|
## Working in this repo
|
||||||
|
|
||||||
- The skills here are the source. Edit them here, then copy them to `~/.claude/skills/` again. Do not edit the installed copies.
|
- The skills here are the source. Edit them here, then copy them to `~/.claude/skills/` again. Do not edit the installed copies.
|
||||||
- `anti-over-engineering` and `graphify` are third-party and kept as the originals. Do not edit them. `anti-over-engineering` is from agents-inc/skills (MIT, by @vince) and was deleted upstream in March 2026, keep the attribution comment at the top. `graphify` is Apache-2.0, keep its `LICENSE`, `LICENSE-MIT` and `NOTICE`.
|
- `anti-over-engineering`, `graphify` and `rust-best-practices` are third-party. Do not edit the first two: `anti-over-engineering` is from agents-inc/skills (MIT, by @vince) and was deleted upstream in March 2026, keep the attribution comment at the top. `graphify` is Apache-2.0, keep its `LICENSE`, `LICENSE-MIT` and `NOTICE`. `rust-best-practices` is Apache-2.0 from apollographql/rust-best-practices, with the changes listed at the top of its `SKILL.md`; keep `LICENSE` and `SOURCE`.
|
||||||
- `firka-style` and `firka-style-website` are ours and do not repeat the other two.
|
- `firka-style` and `firka-style-website` are ours and do not repeat the other two.
|
||||||
- Commits: plain and human. No mention of Claude or any AI in a message, no `Co-Authored-By`. Use the format in `skills/firka-style/SKILL.md`, section 9.
|
- Commits: plain and human. No mention of Claude or any AI in a message, no `Co-Authored-By`. Use the format in `skills/firka-style/SKILL.md`, section 9.
|
||||||
- Do not commit or push unless asked.
|
- Do not commit or push unless asked.
|
||||||
|
|||||||
201
skills/rust-best-practices/LICENSE
Normal file
201
skills/rust-best-practices/LICENSE
Normal file
@@ -0,0 +1,201 @@
|
|||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
148
skills/rust-best-practices/SKILL.md
Normal file
148
skills/rust-best-practices/SKILL.md
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
---
|
||||||
|
name: rust-best-practices
|
||||||
|
description: Rules for writing and reviewing idiomatic Rust (ownership, Option/Result flow, errors, iterators, clippy, testing, docs, dispatch). Use whenever writing, changing or reviewing Rust code. Handbook chapters with examples are in references/.
|
||||||
|
---
|
||||||
|
|
||||||
|
<!-- From apollographql/rust-best-practices (0727a45), Apache-2.0, see LICENSE. Changed: Codex frontmatter
|
||||||
|
replaced, Rule 16 trimmed to the parts that are not about Apollo's own repo, review-reference links removed. -->
|
||||||
|
|
||||||
|
# Rust best practices
|
||||||
|
|
||||||
|
Apply these rules when writing or reviewing Rust. The handbook chapters in `references/` explain each rule with examples; read the matching chapter when a rule is unclear.
|
||||||
|
|
||||||
|
## Rule 1: Borrow by default
|
||||||
|
|
||||||
|
- Default to borrowing (`&T`, `&mut T`) and avoid cloning.
|
||||||
|
- Use owned inputs only when semantics or API require it.
|
||||||
|
- Public APIs should prefer `&str` / `&[T]` over `String` / `Vec<T>` unless ownership is needed.
|
||||||
|
- Prefer `if`/`else if`/`else` over early-return-heavy control flow when that improves readability.
|
||||||
|
|
||||||
|
## Rule 2: Copy/value semantics intentionally
|
||||||
|
|
||||||
|
- Pass small `Copy` types by value.
|
||||||
|
- Avoid passing large structs/heap-backed values by value in public APIs.
|
||||||
|
- Do not mark large complex types as `Copy`.
|
||||||
|
|
||||||
|
## Rule 3: Option/Result control flow
|
||||||
|
|
||||||
|
- Prefer `let PATTERN = EXPR else { ... }` for early expected exits.
|
||||||
|
- Use `if let ... else` when extra logic is required in both branches.
|
||||||
|
- Use `?` for normal propagation.
|
||||||
|
- Keep `unwrap`/`expect` restricted to tests or impossibility proofs.
|
||||||
|
- Avoid silent recovery from `Err`; preserve root-cause visibility where recovery is needed.
|
||||||
|
- In production fallible paths, return `Result` with meaningful context instead of aborting.
|
||||||
|
- If `expect` is used, include invariant-focused messages.
|
||||||
|
|
||||||
|
## Rule 4: Prevent eager allocation
|
||||||
|
|
||||||
|
- Use `_else` methods for costly defaults.
|
||||||
|
- Defer conversions and allocations until consumed.
|
||||||
|
- Pass iterators through pipelines instead of collecting then reprocessing.
|
||||||
|
- Use `_or_else` to delay expensive default creation.
|
||||||
|
|
||||||
|
## Rule 5: Iterator and loop policy
|
||||||
|
|
||||||
|
- Prefer iterator chains for pure transformations.
|
||||||
|
- Use `for` when requiring `break`, `continue`, `return`, or complex side effects.
|
||||||
|
- Keep transform chains readable and line-broken.
|
||||||
|
|
||||||
|
## Rule 6: Imports and style
|
||||||
|
|
||||||
|
- Keep import grouping: std/core/alloc, external crates, workspace, `super::`, `crate::`.
|
||||||
|
- Use stable rustfmt ordering where available.
|
||||||
|
- Keep side effects at system boundaries; keep transformation logic side-effect free.
|
||||||
|
- Keep transform chains readable and line-broken.
|
||||||
|
- Prefer functional composition for pure logic; use mutable/shared state only at boundaries.
|
||||||
|
|
||||||
|
## Rule 7: Clippy-first workflow
|
||||||
|
|
||||||
|
- Use: `cargo clippy --all-targets --all-feature --locked -- -D warnings`.
|
||||||
|
- Treat clippy warnings as actionable.
|
||||||
|
- Avoid blanket suppression; if necessary, prefer documented local `#[expect(...)]`.
|
||||||
|
|
||||||
|
## Rule 8: Performance only after measurement
|
||||||
|
|
||||||
|
- Don't optimize by intuition.
|
||||||
|
- Measure with release builds, profiling, and benchmarks.
|
||||||
|
- Prefer measured alternatives when optimization claim is made.
|
||||||
|
- Performance changes should be backed by evidence.
|
||||||
|
|
||||||
|
## Rule 9: Error strategy
|
||||||
|
|
||||||
|
- Library crates: prefer typed errors (`thiserror`) and crate-level variants.
|
||||||
|
- Binaries: `anyhow` acceptable for ergonomics, with consistent context.
|
||||||
|
- Preserve error context where callers need it.
|
||||||
|
- Avoid silent recovery from `Err` unless intentionally handled.
|
||||||
|
- Prefer explicit boundary conversions for typed errors.
|
||||||
|
|
||||||
|
## Rule 10: Public API docs
|
||||||
|
|
||||||
|
- Document public items with `///` and include:
|
||||||
|
- what it does,
|
||||||
|
- parameters and outputs,
|
||||||
|
- errors, panics, and safety notes when relevant,
|
||||||
|
- examples.
|
||||||
|
- Use `//!` for module/crate-level purpose.
|
||||||
|
- Keep comments for intent, assumptions, constraints, or safety rationale; remove stale comments.
|
||||||
|
|
||||||
|
## Rule 11: Testing quality
|
||||||
|
|
||||||
|
- One test should validate one behavior.
|
||||||
|
- Use descriptive sentence-like names.
|
||||||
|
- Use both unit and integration coverage for behavioral and contract verification.
|
||||||
|
- Use snapshot testing for complex structured outputs only.
|
||||||
|
- Required quality gates:
|
||||||
|
- `cargo fmt --all --check`
|
||||||
|
- `cargo clippy --workspace --all-targets --all-features -- -D warnings`
|
||||||
|
- `cargo test --workspace --all-features`
|
||||||
|
- Pre-merge checklist:
|
||||||
|
- intentional ownership/data-flow review
|
||||||
|
- explicit and aligned error handling
|
||||||
|
- measured performance claim verification
|
||||||
|
- documentation reflects runtime behavior
|
||||||
|
|
||||||
|
## Rule 12: Dispatch choice
|
||||||
|
|
||||||
|
- Prefer static dispatch (`impl Trait` / generic bounds) when types are known and performance matters.
|
||||||
|
- Use dynamic dispatch (`dyn Trait`) for plugin-style/runtime polymorphism needs.
|
||||||
|
- Delay boxing until required by design boundaries.
|
||||||
|
|
||||||
|
## Rule 13: Type-state when valuable
|
||||||
|
|
||||||
|
- Use type-state when illegal states should be compile-time errors.
|
||||||
|
- Skip type-state for simple enums/flags where runtime checks are clearer.
|
||||||
|
|
||||||
|
## Rule 14: Comments and TODO policy
|
||||||
|
|
||||||
|
- Keep comments for intent, constraints, safety, or architecture rationale.
|
||||||
|
- Remove stale comments and avoid obvious-restatement comments.
|
||||||
|
- Convert TODOs into issue references: `// TODO(issue #NNN): ...`
|
||||||
|
- Do not add comments for explicit code changes unless required for intent or safety.
|
||||||
|
- Avoid unjustified `#[allow]`; prefer local `#[expect(...)]` only when explicitly needed.
|
||||||
|
|
||||||
|
## Rule 15: Pointer and concurrency model
|
||||||
|
|
||||||
|
- Prefer references over pointers.
|
||||||
|
- Use `Arc`/`Rc` only by threading requirement.
|
||||||
|
- Use `Cell`/`RefCell` only when borrow rules must be relaxed.
|
||||||
|
- Treat raw pointer usage as unsafe API surface requiring explicit invariants.
|
||||||
|
- Default to borrowing (`&T`, `&mut T`) and avoid cloning unless ownership is required.
|
||||||
|
- Use small `Copy` types by value and pass heap-backed or large types by reference.
|
||||||
|
- Prefer `From`/`Into` over manual bit-level conversions.
|
||||||
|
|
||||||
|
## Rule 16: Workflow
|
||||||
|
|
||||||
|
- Never "clean up", normalize, or replace in-progress experiments unless explicitly asked.
|
||||||
|
- Check `git status --short --branch` before starting work.
|
||||||
|
- Prefer explicitness over magic; extract identifier/key strings into named constants.
|
||||||
|
- Keep code paths simple and explicit before adding clever abstractions.
|
||||||
|
- Never introduce new `panic!`, `unwrap`, or `expect` in shipping code unless explicitly approved.
|
||||||
|
|
||||||
|
## Extra rules
|
||||||
|
|
||||||
|
- Avoid:
|
||||||
|
- unnecessary cloning in hot paths
|
||||||
|
- unjustified `#[allow]`
|
||||||
|
- missing context on errors
|
||||||
|
- copying large values by value without justification
|
||||||
|
- TODOs without ownership/context
|
||||||
3
skills/rust-best-practices/SOURCE
Normal file
3
skills/rust-best-practices/SOURCE
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
Rust best practices skill, built from apollographql/rust-best-practices (commit 0727a45).
|
||||||
|
Upstream: https://github.com/apollographql/rust-best-practices (Apache-2.0, see LICENSE).
|
||||||
|
SKILL.md is .codex/rules/rust_programming.md with the changes listed at its top. references/ is book/ unchanged.
|
||||||
657
skills/rust-best-practices/references/chapter_01.md
Normal file
657
skills/rust-best-practices/references/chapter_01.md
Normal file
@@ -0,0 +1,657 @@
|
|||||||
|
# Chapter 1 - Coding Styles and Idioms
|
||||||
|
|
||||||
|
## 1.1 Borrowing Over Cloning
|
||||||
|
|
||||||
|
Rust’s ownership system encourages **borrow** (`&T`) instead of **cloning** (`T.clone()`).
|
||||||
|
> ❗ Performance recommendation
|
||||||
|
|
||||||
|
### ✅ When to `Clone`:
|
||||||
|
|
||||||
|
* You need to change the object AND preserve the original object (immutable snapshots).
|
||||||
|
* When you have `Arc` or `Rc` pointers.
|
||||||
|
* When data is shared across threads, usually `Arc`.
|
||||||
|
* Avoid massive refactoring of non performance critical code.
|
||||||
|
* When caching results (dummy example below):
|
||||||
|
```rust
|
||||||
|
fn get_config(&self) -> Config {
|
||||||
|
self.cached_config.clone()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* When the underlying API expects Owned Data.
|
||||||
|
|
||||||
|
### 🚨 `Clone` traps to avoid:
|
||||||
|
|
||||||
|
* Auto-cloning inside loops `.map(|x| x.clone)`, prefer to call `.cloned()` or `.copied()` at the end of the iterator.
|
||||||
|
* Cloning large data structures like `Vec<T>` or `HashMap<K, V>`.
|
||||||
|
* Clone because of bad API design instead of adjusting lifetimes.
|
||||||
|
* Prefer `&[T]` instead of `Vec<T>` or `&Vec<T>`.
|
||||||
|
* Prefer `&str` or `&String` instead of `String`.
|
||||||
|
* Prefer `&T` instead of `T`.
|
||||||
|
* Clone a reference argument, if you need ownership, make it explicit in the arguments for the caller. Example:
|
||||||
|
```rust
|
||||||
|
fn take_a_borrow(thing: &Thing) {
|
||||||
|
let thing_cloned = thing.clone(); // the caller should have passed ownership instead
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ✅ Prefer borrowing:
|
||||||
|
```rust
|
||||||
|
fn process(name: &str) {
|
||||||
|
println!(“Hello {name}”);
|
||||||
|
}
|
||||||
|
|
||||||
|
let user = String::from(“foo”);
|
||||||
|
process(&user);
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❌ Avoid redundant cloning:
|
||||||
|
```rust
|
||||||
|
fn process_string(name: String) {
|
||||||
|
println!(“Hello {name}”);
|
||||||
|
}
|
||||||
|
|
||||||
|
let user = String::from(“foo”);
|
||||||
|
process(user.clone()); // Unnecessary clone
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1.2 When to pass by value? (Copy trait)
|
||||||
|
|
||||||
|
Not all types should be passed by reference (`&T`). If a type is **small** and it is **cheap to copy**, it is often better to **pass it by value**. Rust makes it explicit via the `Copy` trait.
|
||||||
|
|
||||||
|
### ✅ When to pass by value, `Copy`:
|
||||||
|
* The type **implements** `Copy` (`u32`, `bool`, `f32`, small structs).
|
||||||
|
* The cost of moving the value is negligible.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn increment(x: u32) -> u32 {
|
||||||
|
x + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
let num = 1;
|
||||||
|
let new_num = increment(num); // `num` still usable after this point
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❓ Which structs should be `Copy`?
|
||||||
|
* When to consider declaring `Copy` on your own types:
|
||||||
|
* All fields are `Copy` themselves.
|
||||||
|
* The struct is `small`, up to 2 (maybe 3) words of memory or 24 bytes (each word is 64 bits/8bytes).
|
||||||
|
* The struct **represents a “plain data object”**, without resorting to ownership (no heap allocations. Example: `Vec` and `Strings`).
|
||||||
|
* ❗**The type does not also implement `Iterator`.** Even if every field is `Copy`, never put `Copy` and `Iterator` on the same type (see [§1.5](#15-iterator-iter-vs-for)).
|
||||||
|
|
||||||
|
❗**Rust Arrays are stack allocated.** Which means they can be copied if their underlying type is `Copy`, but this will be allocated in the program stack which can easily become a stack overflow. More on [Chapter 3 - Stack vs Heap](https://github.com/apollographql/rust-best-practices/blob/main/book/chapter_03.md#33-stack-vs-heap-be-size-smart)
|
||||||
|
|
||||||
|
For reference, each primitive type size in bytes:
|
||||||
|
|
||||||
|
#### Integers:
|
||||||
|
|
||||||
|
| Type | Size |
|
||||||
|
|------------- |---------- |
|
||||||
|
| i8 u8 | 1 byte |
|
||||||
|
| i16 u16 | 2 bytes |
|
||||||
|
| i32 u32 | 4 bytes |
|
||||||
|
| i64 u64 | 8 bytes |
|
||||||
|
| isize usize | Arch |
|
||||||
|
| i128 u128 | 16 bytes |
|
||||||
|
|
||||||
|
#### Floating Point:
|
||||||
|
|
||||||
|
| Type | Size |
|
||||||
|
|---------- |---------- |
|
||||||
|
| f32 | 4 bytes |
|
||||||
|
| f64 | 8 bytes |
|
||||||
|
|
||||||
|
|
||||||
|
#### Other:
|
||||||
|
|
||||||
|
| Type | Size |
|
||||||
|
|---------- |---------- |
|
||||||
|
| bool | 1 byte |
|
||||||
|
| char | 4 bytes |
|
||||||
|
|
||||||
|
|
||||||
|
### ✅ Good struct to derive `Copy`:
|
||||||
|
```rust
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
struct Point {
|
||||||
|
x: f32,
|
||||||
|
y: f32,
|
||||||
|
z: f32
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❌ Bad struct to derive `Copy`:
|
||||||
|
```rust
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct BadIdea {
|
||||||
|
age: i32,
|
||||||
|
name: String, // String is not `Copy`
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❓Which Enums should be `Copy`?
|
||||||
|
* If your enum acts like tags and atoms.
|
||||||
|
* The enum payloads are all `Copy`.
|
||||||
|
* **❗Enums size are based on their largest element.**
|
||||||
|
|
||||||
|
### ✅ Good Enum to derive
|
||||||
|
```rust
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
enum Direction {
|
||||||
|
North,
|
||||||
|
South,
|
||||||
|
East,
|
||||||
|
West,
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1.3 Handling `Option<T>` and `Result<T, E>`
|
||||||
|
Rust 1.65 introduced a better way to safely unpack Option and Result types with the `let Some(x) = … else { … }` or `let Ok(x) = … else { … }` when you have a default `return` value, `continue` or `break` default else case. It allows early returns when the missing case is **expected and normal**, not exceptional.
|
||||||
|
|
||||||
|
### ✅ Cases to use each pattern matching for Option and Result
|
||||||
|
* Use `match` when you want to pattern match against the inner types `T` and `E`
|
||||||
|
```rust
|
||||||
|
match self {
|
||||||
|
Ok(Direction::South) => { … },
|
||||||
|
Ok(Direction::North) => { … },
|
||||||
|
Ok(Direction::East) => { … },
|
||||||
|
Ok(Direction::West) => { … },
|
||||||
|
Err(E::One) => { … },
|
||||||
|
Err(E::Two) => { … },
|
||||||
|
}
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Some(3|5) => { … }
|
||||||
|
Some(x) if x > 10 => { … }
|
||||||
|
Some(x) => { … }
|
||||||
|
None => { … }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Use `match` when your type is transformed into something more complex Like `Result<T, E>` becoming `Result<Option<U>, E>`.
|
||||||
|
```rust
|
||||||
|
match self {
|
||||||
|
Ok(t) => Ok(Some(t)),
|
||||||
|
Err(E::Empty) => Ok(None),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Use `let PATTERN = EXPRESSION else { DIVERGING_CODE; }` when the divergent code doesn’t need to know about the failed pattern matches or doesn’t need extra computation:
|
||||||
|
```rust
|
||||||
|
let Some(&Direction::North) = self.direction.as_ref() else {
|
||||||
|
return Err(DirectionNotAvailable(self.direction));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Use `let PATTERN = EXPRESSION else { DIVERGING_CODE; }` when you want to break or continue a pattern match
|
||||||
|
```rust
|
||||||
|
for x in self {
|
||||||
|
let Some(x) = x else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Use `if let PATTERN = EXPRESSION else { DIVERGING_CODE; }` when `DIVERGING_CODE` needs extra computation:
|
||||||
|
```rust
|
||||||
|
if let Some(x) = self.next() {
|
||||||
|
// computation
|
||||||
|
} else {
|
||||||
|
// computation when `None/Err` or not matched
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
❗**If you don’t care about the value of the `Err` case, please use `?` to propagate the `Err` to the caller.**
|
||||||
|
|
||||||
|
### ❌ Bad Option/Result pattern matching:
|
||||||
|
|
||||||
|
* Conversion between Result and Option (prefer `.ok()`,`.ok_or()`, and `ok_or_else()`)
|
||||||
|
```rust
|
||||||
|
match self {
|
||||||
|
Ok(t) => Some(t),
|
||||||
|
Err(_) => None
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* `if let PATTERN = EXPRESSION else { DIVERGING_CODE; }` when divergent code is a default or pre-computed value (prefer `let PATTERN = EXPRESSION else { DIVERGING_CODE; }`):
|
||||||
|
```rust
|
||||||
|
if let Some(values) = self.next() {
|
||||||
|
// computation
|
||||||
|
(Some(..), values)
|
||||||
|
} else {
|
||||||
|
(None, Vec::new())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Using `unwrap` or `expect` outside tests:
|
||||||
|
```rust
|
||||||
|
let port = config.port.unwrap();
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1.4 Prevent Early Allocation
|
||||||
|
|
||||||
|
When dealing with functions like `or`, `map_or`, `unwrap_or`, `ok_or`, consider that they have special cases for when memory allocation is required, like creating a new string, creating a collection or even calling functions that manage some state, so they can be replaced with their `_else` counter-part:
|
||||||
|
|
||||||
|
### ✅ Good cases
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let x = None;
|
||||||
|
assert_eq!(x.ok_or(ParseError::ValueAbsent), Err(ParseError::ValueAbsent));
|
||||||
|
|
||||||
|
let x = None;
|
||||||
|
assert_eq!(x.ok_or_else(|| ParseError::ValueAbsent(format!("this is a value {x}"))), Err(ParseError::ValueAbsent));
|
||||||
|
|
||||||
|
|
||||||
|
let x: Result<_, &str> = Ok("foo");
|
||||||
|
assert_eq!(x.map_or(42, |v| v.len()), 3);
|
||||||
|
|
||||||
|
|
||||||
|
let x : Result<_, String> = Ok("foo");
|
||||||
|
assert_eq!(x.map_or_else(|e|format!("Error: {e}"), |v| v.len()), 3);
|
||||||
|
|
||||||
|
let x = "1,2,3,4";
|
||||||
|
assert_eq!(x.parse_to_option_vec.unwrap_or_else(Vec::new), Ok(vec![1, 2, 3, 4]));
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❌ Bad cases
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let x : Result<_, String> = Ok("foo");
|
||||||
|
assert_eq!(x.map_or(format!("Error with uninformed content"), |v| v.len()), 3);
|
||||||
|
|
||||||
|
let x = "1,2,3,4";
|
||||||
|
assert_eq!(x.parse_to_option_vec.unwrap_or(Vec::new()), Ok(vec![1, 2, 3, 4])); // could be replaced with `.unwrap_or_default`
|
||||||
|
|
||||||
|
let x = None;
|
||||||
|
assert_eq!(x.ok_or(ParseError::ValueAbsent(format!("this is a value {x}"))), Err(ParseError::ValueAbsent));
|
||||||
|
```
|
||||||
|
|
||||||
|
### Mapping Err
|
||||||
|
|
||||||
|
When dealing with Result::Err, sometimes is necessary to log and transform the Err into a more abstract or more detailed error, this can be done with `inspect_err` and `map_err`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let x = Err(ParseError::InvalidContent(...));
|
||||||
|
|
||||||
|
x
|
||||||
|
.inspect_err(|err| tracing::error!("function_name: {err}"))
|
||||||
|
.map_err(|err| GeneralError::from(("function_name", err)))?;
|
||||||
|
```
|
||||||
|
|
||||||
|
## 1.5 Iterator, `.iter` vs `for`
|
||||||
|
|
||||||
|
First we need to understand a basic loop with each one of them. Let's consider the following problem, we need to sum all even numbers between 0 and 10 incremented by 1:
|
||||||
|
|
||||||
|
* `for`:
|
||||||
|
```rust
|
||||||
|
let mut sum = 0;
|
||||||
|
for x in 0..=10 {
|
||||||
|
if x % 2 == 0 {
|
||||||
|
sum += x + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* `iter`:
|
||||||
|
```rust
|
||||||
|
let sum: i32 = (0..=10)
|
||||||
|
.filter(|x| x % 2 == 0)
|
||||||
|
.map(|x| x + 1)
|
||||||
|
.sum();
|
||||||
|
```
|
||||||
|
|
||||||
|
> Both versions do the same thing and are correct and idiomatic, but each shines in different contexts.
|
||||||
|
|
||||||
|
### When to prefer `for` loops
|
||||||
|
* When you need **early exits** (`break`, `continue`, `return`).
|
||||||
|
* **Simple iteration** with side-effects (e.g., logging, IO)
|
||||||
|
* logging can be done correctly in `Iterators` using `inspect` and `inspect_err` functions.
|
||||||
|
* When readability matters more than simplicity or chaining.
|
||||||
|
|
||||||
|
#### Example:
|
||||||
|
```rust
|
||||||
|
for value in &mut value {
|
||||||
|
if *value == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
*value += fancy_equation();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### When to prefer `iterators` loops (`.iter()` and `.into_iter()`)
|
||||||
|
* When you are `transforming collections` or `Option/Results`.
|
||||||
|
* You can **compose multiple steps** elegantly.
|
||||||
|
* No need for early exits.
|
||||||
|
* You need support for indexed values with `.enumerate`.
|
||||||
|
```rust
|
||||||
|
let values: Vec<_> = vec.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_index, value)| value % 2 == 0)
|
||||||
|
.map(|(index, value)| value % index)
|
||||||
|
.collect()
|
||||||
|
```
|
||||||
|
* You need to use collections functions like `.windows` or `chunks`.
|
||||||
|
* You need to combine data from multiple sources and don't want to allocate multiple collections.
|
||||||
|
* Iterators can be combined with `for` loops:
|
||||||
|
```rust
|
||||||
|
for value in vec.iter().enumerate()
|
||||||
|
.filter(|(index, value)| value % index == 0) {
|
||||||
|
// ...
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
> #### ❗REMEMBER: Iterators are Lazy
|
||||||
|
>
|
||||||
|
> * `.iter`, `.map`, `.filter` don't do anything until you call its consumer, e.g. `.collect`, `.sum`, `.for_each`.
|
||||||
|
> * **Lazy Evaluation** means that iterator chains are fused into one loop at compile time.
|
||||||
|
|
||||||
|
### 🚨 Anti-patterns to AVOID
|
||||||
|
|
||||||
|
* Don't chain without formatting. Prefer each chained function on its own line with the correct indentation (`rustfmt` should take care of this).
|
||||||
|
* Don't chain if it makes the code unreadable.
|
||||||
|
* Avoid needlessly collect/allocate of a collection (e.g. vector) just to throw it away later by some larger operation or by another iteration.
|
||||||
|
* Prefer `iter` over `into_iter` unless you don't need the ownership of the collection.
|
||||||
|
* Prefer `iter` over `into_iter` for collections that inner type implements `Copy`, e.g. `Vec<T: Copy>`.
|
||||||
|
* **Never implement (or derive) both `Copy` and `Iterator` on the same type.** Copying an iterator and advancing one copy leaves the other untouched, which silently yields wrong results — it is a well-known footgun. The standard library hit exactly this: it is why `Range` historically could not be `Copy`, and why the new `core::range` types (stabilized in Rust 1.96) implement `IntoIterator` instead of `Iterator` so they *can* be `Copy`. If you need an iterator on a `Copy` type, implement `IntoIterator` and return a separate iterator struct.
|
||||||
|
* For summing numbers prefer `.sum` over `.fold`. `.sum` is specialized for summing values, so the compiler knows it can make optimizations on that front, while fold has a blackbox closure that needs to be applied at every step. If you need to sum by an initial value, just add it in the expression `let my_sum = [1, 2, 3].sum() + 3`.
|
||||||
|
|
||||||
|
## 1.6 Comments: Context, not Clutter
|
||||||
|
|
||||||
|
> "Comments are for why, not what or how"
|
||||||
|
|
||||||
|
Well-written Rust code, with expressive types and good naming, often speaks for itself. Many high-quality codebases thrive on **few or no comments**. And that's a good thing.
|
||||||
|
|
||||||
|
Still, there are **moments where code alone isn't enough** - when there are performance quirks, external constraints, or non-obvious tradeoffs that require a nudge to the reader. In those cases, a concise comment can prevent hours of head-scratching or searching git history.
|
||||||
|
|
||||||
|
### ✅ Good comments
|
||||||
|
|
||||||
|
* Safety concerns:
|
||||||
|
```rust
|
||||||
|
// SAFETY: We have checked that the pointer is valid and non-null. @Function xyz.
|
||||||
|
unsafe { std::ptr::copy_nonoverlapping(src, dst, len); }
|
||||||
|
```
|
||||||
|
|
||||||
|
* Performance quirks:
|
||||||
|
```rust
|
||||||
|
// This algorithm is a fast square root approximation
|
||||||
|
const THREE_HALVES: f32 = 1.5;
|
||||||
|
fn q_rsqrt(number: f32 ) -> f32 {
|
||||||
|
let mut i: i32 = number.to_bits() as i32;
|
||||||
|
i = 0x5F375A86_i32.wrapping_sub(i >> 1);
|
||||||
|
let y = f32::from_bits(i as u32);
|
||||||
|
y * (THREE_HALVES - (number * 0.5 * y * y))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Clear code beats comments. However, when the why isn't obvious, say it plainly - or link to where:
|
||||||
|
```rust
|
||||||
|
// PERF: Generating the root store per subgraph caused high TLS startup latency on MacOS
|
||||||
|
// This works as a caching alternative. See: [ADR-123](link/to/adr-123)
|
||||||
|
let subgraph_tls_root_store: RootCertStore = configuration
|
||||||
|
.tls
|
||||||
|
.subgraph
|
||||||
|
.all
|
||||||
|
.create_certificate_store()
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or_else(crate::services::http::HttpClientService::native_roots_store);
|
||||||
|
let connector_tls_root_store: RootCertStore = configuration
|
||||||
|
.tls
|
||||||
|
.connector
|
||||||
|
.all
|
||||||
|
.create_certificate_store()
|
||||||
|
.transpose()?
|
||||||
|
.unwrap_or_else(crate::services::http::HttpClientService::native_roots_store);
|
||||||
|
```
|
||||||
|
|
||||||
|
* ❗ More use cases to come in their appropriate sections.
|
||||||
|
|
||||||
|
### ❌ Bad comments
|
||||||
|
|
||||||
|
* Wall-of-text explanations: long comments and multiline comments
|
||||||
|
```rust
|
||||||
|
// Lorem Ipsum is simply dummy text of the printing and typesetting industry.
|
||||||
|
// Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
|
||||||
|
// when an unknown printer took a galley
|
||||||
|
fn do_something_odd() {
|
||||||
|
…
|
||||||
|
}
|
||||||
|
```
|
||||||
|
> Prefer `/// doc` comment if it's describing the function.
|
||||||
|
|
||||||
|
* Comments that could be better represented as functions or are plain obvious
|
||||||
|
```rust
|
||||||
|
fn computation() {
|
||||||
|
// increment i by 1
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ✅ Breaking up long functions over commenting them
|
||||||
|
|
||||||
|
If you find yourself writing a long comment explaining "what", "how" or "each step" in a function, it might be time to split it. So the suggestion is to refactor. This can be beneficial not only for readability, but testability:
|
||||||
|
|
||||||
|
#### ❌ Instead of:
|
||||||
|
```rust
|
||||||
|
fn process_request(request: T) {
|
||||||
|
// We first need to validate request, because of corner case x, y, z
|
||||||
|
// As the payload can only be decoded when they are valid
|
||||||
|
// Then we can perform authorization on the payload
|
||||||
|
// lastly with the authorized payload we can dispatch to handler
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### ✅ Prefer
|
||||||
|
```rust
|
||||||
|
fn process_request(request: T) -> Result<(), Error> {
|
||||||
|
validate_request_headers(&request)?;
|
||||||
|
let payload = decode_payload(&request);
|
||||||
|
authorize(&payload)?;
|
||||||
|
dispatch_to_handler(payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn validate_request_happy_path() { ... }
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_request_fails_on_x() { ... }
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn validate_request_fails_on_y() { ... }
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn decode_validated_request() { ... }
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn authorize_payload_xyz() { ... }
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Let **structure** and **naming** replace commentary, and enhance its documentation with **tests as living documentation**.
|
||||||
|
|
||||||
|
> ❗ Splitting is about **naming and clarity**, not deduplication. Before extracting a helper to remove *duplicated* lines, check [§1.8](#18-when-to-extract-a-function-and-when-not-to).
|
||||||
|
|
||||||
|
### 📝 TODOs are not comments - track them properly
|
||||||
|
|
||||||
|
Avoid leaving lingering `// TODO: Lorem Ipsum` comments in the code. Instead:
|
||||||
|
* Turn them into Jira or Github Issues.
|
||||||
|
* If needed, to avoid future confusion, reference the issue in the code and the code in the issue.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// See issue #123: support hyper 2.0
|
||||||
|
```
|
||||||
|
|
||||||
|
This helps keeping the code clean and making sure tasks are not forgotten.
|
||||||
|
|
||||||
|
### Comments as Living Documentation
|
||||||
|
|
||||||
|
There are a few gotchas when calling comments "living documentation":
|
||||||
|
* Code evolves.
|
||||||
|
* Context changes.
|
||||||
|
* Comments get stale.
|
||||||
|
* Many large comments make people avoid reading them.
|
||||||
|
* Team becomes fearful of deleting irrelevant comments.
|
||||||
|
|
||||||
|
If you find a comment, **don't trust it blindly**. Read it in context. If it's wrong or outdated, fix or remove it. A misleading comment is worse than no comments at all.
|
||||||
|
|
||||||
|
> Comments should bother you - they demand re-verification, just like stale tests.
|
||||||
|
|
||||||
|
When deeper justification is needed, prefer to:
|
||||||
|
* **Link to a Design Doc or an ADR**, business logic lives well in design docs while performance tradeoffs live well in ADRs.
|
||||||
|
* Move runtime example and usage docs into Rust Docs, `/// doc comment`, where they can be tested and kept up-to-date by tools like `cargo doc`.
|
||||||
|
|
||||||
|
> Doc-comments and Doc-testing, `///` and `//!` in [Chapter 8 - Comments vs Documentation](./chapter_08.md)
|
||||||
|
|
||||||
|
## 1.7 Use Declarations - "imports"
|
||||||
|
|
||||||
|
Different languages have different ways of sorting their imports, in the Rust ecosystem the [standard way](https://github.com/rust-lang/rustfmt/issues/4107) is:
|
||||||
|
|
||||||
|
- `std` (`core`, `alloc` would also fit here).
|
||||||
|
- External crates (what is in your Cargo.toml `[dependencies]`).
|
||||||
|
- Workspace crates (workspace member crates).
|
||||||
|
- This module `super::`.
|
||||||
|
- This module `crate::`.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// std
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
// external crates
|
||||||
|
use chrono::Utc;
|
||||||
|
use juniper::{FieldError, FieldResult};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
// crate code lives in workspace
|
||||||
|
use broker::database::PooledConnection;
|
||||||
|
|
||||||
|
// super:: / crate::
|
||||||
|
use super::schema::{Context, Payload};
|
||||||
|
use super::update::convert_publish_payload;
|
||||||
|
use crate::models::Event;
|
||||||
|
```
|
||||||
|
|
||||||
|
Some enterprise solutions opt to include their core packages after `std`, so all external packages that start with enterprise name are located before the others:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// std
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
// enterprise external crates
|
||||||
|
use enterprise_crate_name::some_module::SomeThing;
|
||||||
|
|
||||||
|
// external crates
|
||||||
|
use chrono::Utc;
|
||||||
|
use juniper::{FieldError, FieldResult};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
// crate code lives in workspace
|
||||||
|
use broker::database::PooledConnection;
|
||||||
|
|
||||||
|
// super:: / crate::
|
||||||
|
use super::schema::{Context, Payload};
|
||||||
|
use super::update::convert_publish_payload;
|
||||||
|
use crate::models::Event;
|
||||||
|
```
|
||||||
|
|
||||||
|
One way of not having to manually control this is using the following arguments in your `rustfmt.toml`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
reorder_imports = true
|
||||||
|
imports_granularity = "Crate"
|
||||||
|
group_imports = "StdExternalCrate"
|
||||||
|
```
|
||||||
|
|
||||||
|
> As of Rust version 1.88, it is necessary to execute rustfmt in nightly to correctly reorder code `cargo +nightly fmt`.
|
||||||
|
|
||||||
|
## 1.8 When to Extract a Function (and When Not To)
|
||||||
|
|
||||||
|
This chapter ([§1.6](#-breaking-up-long-functions-over-commenting-them)) and [Chapter 8](chapter_08.md#85-replace-comments-with-code) recommend splitting long functions and replacing narrative comments with named helpers. That advice is about **naming and clarity** — it is *not* a license to hunt down every repeated line. Extraction has a cost: every helper adds indirection, and a **wrong abstraction is much harder to remove than a little duplication**.
|
||||||
|
|
||||||
|
> "Duplication is far cheaper than the wrong abstraction." — [Sandi Metz, The Wrong Abstraction](https://sandimetz.com/blog/2016/1/20/the-wrong-abstraction)
|
||||||
|
|
||||||
|
### 📏 The Rule of Three
|
||||||
|
|
||||||
|
From [Martin Fowler's *Refactoring*](https://martinfowler.com/books/refactoring.html), attributed to Don Roberts:
|
||||||
|
|
||||||
|
> The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor.
|
||||||
|
|
||||||
|
Two occurrences of a couple of lines are usually **fine**. Wait for the third before reaching for a helper — by then you will know what the abstraction actually *is*, instead of guessing at it.
|
||||||
|
|
||||||
|
### 🧠 DRY is about knowledge, not text
|
||||||
|
|
||||||
|
[DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself) (*The Pragmatic Programmer*) says every piece of **knowledge** should have a single representation. It does **not** say "no two code fragments may look alike". Two blocks that *happen* to look the same today, but represent **different decisions**, will evolve in different directions. Forcing them into one helper couples code that should be free to diverge — this is **coincidental duplication**, and deduplicating it is how wrong abstractions are born.
|
||||||
|
|
||||||
|
### 🧪 Examples
|
||||||
|
|
||||||
|
How these principles play out in code:
|
||||||
|
|
||||||
|
#### ✅ Coincidental duplication — leave it inline:
|
||||||
|
```rust
|
||||||
|
// Similar-looking lines, but the `.env` and `.toml` formats are
|
||||||
|
// unrelated decisions that will evolve independently:
|
||||||
|
writeln!(env_file, "{key}={value}")?;
|
||||||
|
// ... elsewhere ...
|
||||||
|
writeln!(toml_file, "{key} = \"{value}\"")?;
|
||||||
|
```
|
||||||
|
|
||||||
|
#### ❌ Deduplicating it forces a flag parameter — the classic smell:
|
||||||
|
```rust
|
||||||
|
// One helper, two behaviors, and a `bool` to pick between them:
|
||||||
|
fn write_entry(out: &mut impl Write, key: &str, value: &str, quoted: bool) -> io::Result<()> {
|
||||||
|
if quoted {
|
||||||
|
writeln!(out, "{key} = \"{value}\"")
|
||||||
|
} else {
|
||||||
|
writeln!(out, "{key}={value}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Every new variant will grow another parameter or branch. And the `bool` is a smell in its own right: `write_entry(out, key, value, true)` tells the reader nothing at the call site — Martin Fowler calls this a [Flag Argument](https://martinfowler.com/bliki/FlagArgument.html). When a mode selector is genuinely warranted, a two-variant enum (`Quoting::Quoted`) keeps call sites readable, and Clippy's [`fn_params_excessive_bools`](https://rust-lang.github.io/rust-clippy/master/index.html#fn_params_excessive_bools) lints against accumulating them. No enum rescues *this* design, though: its variants would exist only to encode the callers' differences.
|
||||||
|
|
||||||
|
#### ✅ Shared knowledge — extract it:
|
||||||
|
```rust
|
||||||
|
// The definition of "retryable" is ONE business decision used in many
|
||||||
|
// places. If it changes, it must change everywhere at once:
|
||||||
|
fn is_retryable(status: StatusCode) -> bool {
|
||||||
|
status == StatusCode::TOO_MANY_REQUESTS || status.is_server_error()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🔧 Unwinding a wrong abstraction
|
||||||
|
|
||||||
|
If you find yourself maintaining an abstraction like `write_entry`, [Sandi Metz's advice](https://sandimetz.com/blog/2016/1/20/the-wrong-abstraction) is to **unwind it, not patch it**:
|
||||||
|
|
||||||
|
1. **Re-inline** the helper's body into every caller, substituting each call site's concrete arguments.
|
||||||
|
2. **Reduce** each call site — with the parameters now literal, dead branches and unused generality fall away.
|
||||||
|
3. **Reevaluate** what the callers *actually* share, now that every one of them is explicit.
|
||||||
|
4. **Reconstruct** one or more abstractions from what remains — or none, if the sharing turns out to be coincidental.
|
||||||
|
|
||||||
|
> 🤖 Doing this by hand is tedious, which is why wrong abstractions survive on sunk cost. For an AI coding agent, unwinding is fast, mechanical work — ask for it explicitly instead of letting the agent keep patching a helper that fights back.
|
||||||
|
|
||||||
|
### ✅ Extract a function when:
|
||||||
|
* The logic appears in **3+ places** *and* represents the **same decision** (Rule of Three).
|
||||||
|
* The name **adds meaning** the code alone does not (`is_retryable`, `authorize`) — rather than restating it (`add_one_to_counter`).
|
||||||
|
* It produces a **unit worth testing** in isolation (see [§1.6](#-breaking-up-long-functions-over-commenting-them)).
|
||||||
|
* It hides genuine complexity behind a **small, honest signature** — few parameters, no flags.
|
||||||
|
|
||||||
|
### ❌ Don't extract when:
|
||||||
|
* It is **1–2 lines used in fewer than 3 places** — the helper is pure indirection.
|
||||||
|
* The call sites are only *almost* identical, and unifying them requires **flag parameters** or extra branches.
|
||||||
|
* The only motivation is **line count**. A single-caller helper pays off when its *name* clarifies intent ([§1.6](#-breaking-up-long-functions-over-commenting-them), [§8.5](chapter_08.md#85-replace-comments-with-code)) — not when it merely relocates code.
|
||||||
|
* You are **guessing** at a future abstraction. Wait for the third usage to reveal its real shape.
|
||||||
|
|
||||||
|
### 🧫 Test code: readability beats DRY
|
||||||
|
|
||||||
|
In tests, be **even more tolerant of duplication**. The Google Testing Blog calls this [DAMP — "Descriptive And Meaningful Phrases"](https://testing.googleblog.com/2019/12/testing-on-toilet-tests-too-dry-make.html) ([*Software Engineering at Google*, ch. 12](https://abseil.io/resources/swe-book/html/ch12.html)): each test should read as a **self-contained story** — setup, action, assertion — without the reader chasing helpers to reconstruct what actually happened.
|
||||||
|
|
||||||
|
* **Tests have no tests.** Logic moved into a shared helper (loops, branches, clever parametrization) is itself untested — a bug there silently weakens every test built on it.
|
||||||
|
* **A failing test should be diagnosable on sight.** The reader is usually debugging a broken build; make the expected behavior visible in the test body, not three helpers away.
|
||||||
|
* **Shared test helpers couple unrelated tests.** Change one behavior and dozens of tests fail at once — for the helper's sake, not the behavior's.
|
||||||
|
|
||||||
|
Where to draw the line:
|
||||||
|
* ✅ Share **setup and fixtures** — a shared setup function or `rstest` cases, as [Chapter 5](chapter_05.md#51-tests-as-living-documentation) recommends. Constructing a test server twice is boilerplate, not knowledge.
|
||||||
|
* ❌ Keep each test's **action and assertion inline**, even when they look repetitive across tests.
|
||||||
|
|
||||||
|
> 🚨 When in doubt, **prefer duplication**. A duplicated line is trivially fixed later; a wrong abstraction accretes parameters and conditionals, because each maintainer keeps patching it instead of undoing it.
|
||||||
117
skills/rust-best-practices/references/chapter_02.md
Normal file
117
skills/rust-best-practices/references/chapter_02.md
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
# Chapter 2 - Clippy and Linting Discipline
|
||||||
|
|
||||||
|
Be sure to have `cargo clippy` installed with your rust compiler, run `cargo clippy -V` in your terminal for a rust project and you should get something like this `clippy 0.1.86 (05f9846f89 2025-03-31)`. If terminal fails to show a clippy version, please run the following code `rustup update && rustup component add clippy`.
|
||||||
|
|
||||||
|
Clippy documentation can be found [here](https://doc.rust-lang.org/clippy/usage.html).
|
||||||
|
|
||||||
|
## 2.1 Why care about linting?
|
||||||
|
|
||||||
|
Rust compiler is a powerful tool that catches many mistakes. However, some more in-depth analysis require extra tools, that is where `cargo clippy` comes into play. Clippy checks for:
|
||||||
|
* Performance pitfalls.
|
||||||
|
* Style issues.
|
||||||
|
* Redundant code.
|
||||||
|
* Potential bugs.
|
||||||
|
* Non-idiomatic Rust.
|
||||||
|
|
||||||
|
## 2.2 Always run `cargo clippy`
|
||||||
|
|
||||||
|
Add the following to your daily workflow:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ cargo clippy --all-targets --all-features --locked -- -D warnings
|
||||||
|
```
|
||||||
|
|
||||||
|
* `--all-targets`: checks library, tests, benches and examples.
|
||||||
|
* `--all-features`: checks code with all features enabled; it does not auto-solve conflicting features.
|
||||||
|
* `--locked`: Requires `Cargo.lock` to be up-to-date, can be solved with `$ cargo update`.
|
||||||
|
* `-D warnings`: treats warnings as errors
|
||||||
|
|
||||||
|
Potential additional elements to add:
|
||||||
|
|
||||||
|
* `-- -W clippy::pedantic`: lints which are rather strict or have occasional false positives.
|
||||||
|
* `-- -W clippy::nursery`: Optionally can be added to check for new lints that are still under development.
|
||||||
|
* ❗ Add this to your Makefile, Justfile, xtask or CI Pipeline.
|
||||||
|
|
||||||
|
> Example at ApolloGraphQL
|
||||||
|
>
|
||||||
|
> In the `Router` project there is a `xtask` configured for linting that can be executed with `cargo xtask lint`.
|
||||||
|
|
||||||
|
## 2.3 Important Clippy Lints to Respect
|
||||||
|
|
||||||
|
| Lint Name | Why | Link |
|
||||||
|
| --------- | ----| -----|
|
||||||
|
| `redundant_clone` | Detects unnecessary `clones`, has performance impact | [link (nursery + perf)](https://rust-lang.github.io/rust-clippy/master/#redundant_clone) |
|
||||||
|
| `needless_borrow` group | Removes redundant `&` borrowing | [link (style)](https://rust-lang.github.io/rust-clippy/master/#needless_borrow) |
|
||||||
|
| `map_unwrap_or` / `map_or` | Simplifies nested `Option/Result` handling | [`map_unwrap_or`](https://rust-lang.github.io/rust-clippy/master/#map_unwrap_or) [`unnecessary_map_or`](https://rust-lang.github.io/rust-clippy/master/#unnecessary_map_or) [`unnecessary_result_map_or_else`](https://rust-lang.github.io/rust-clippy/master/#unnecessary_result_map_or_else) |
|
||||||
|
| `manual_ok_or` | Suggest using `.ok_or_else` instead of `match` | [link (style)](https://rust-lang.github.io/rust-clippy/master/#manual_ok_or) |
|
||||||
|
| `large_enum_variant` | Warns if an enum has very large variant which is bad for memory. Suggests `Boxing` it | [link (perf)](https://rust-lang.github.io/rust-clippy/master/#large_enum_variant) |
|
||||||
|
| `unnecessary_wraps` | If your function always returns `Some` or `Ok`, you don't need `Option`/`Result` | [link (pedantic)](https://rust-lang.github.io/rust-clippy/master/#unnecessary_wraps) |
|
||||||
|
| `clone_on_copy` | Catches accidental `.clone()` on `Copy` types like `u32` and `bool` | [link (complexity)](https://rust-lang.github.io/rust-clippy/master/#clone_on_copy) |
|
||||||
|
| `needless_collect` | Prevents collecting and allocating an iterator, when allocation is not needed | [link (nursery)](https://rust-lang.github.io/rust-clippy/master/#needless_collect) |
|
||||||
|
|
||||||
|
## 2.4 Fix warnings, don't silence them!
|
||||||
|
|
||||||
|
**NEVER** just `#[allow(clippy::lint_something)]` unless:
|
||||||
|
|
||||||
|
* You **truly understand** why the warning happens and you have a reason why it is better that way.
|
||||||
|
* You **document** why it is being ignored.
|
||||||
|
* ❗ Don't use `allow`, but `expect`, it will give a warning in case the lint is not true anymore, `#[expect(clippy::lint_something)]`.
|
||||||
|
|
||||||
|
### Example:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Faster matching is preferred over size efficiency
|
||||||
|
#[expect(clippy::large_enum_variant)]
|
||||||
|
enum Message {
|
||||||
|
Code(u8),
|
||||||
|
Content([u8; 1024]),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> The fix would be:
|
||||||
|
>
|
||||||
|
> ```rust
|
||||||
|
> // Faster matching is preferred over size efficiency
|
||||||
|
> #[expect(clippy::large_enum_variant)]
|
||||||
|
> enum Message {
|
||||||
|
> Code(u8),
|
||||||
|
> Content(Box<[u8; 1024]>),
|
||||||
|
> }
|
||||||
|
> ```
|
||||||
|
|
||||||
|
### Handling false positives
|
||||||
|
|
||||||
|
Sometimes Clippy complains even when your code is correct, in those cases there are two solutions:
|
||||||
|
1. Try to refactor the code, so it improves the warning.
|
||||||
|
2. **Locally** override the lint with `#[expect(clippy::lint_name)]` and a comment with the reason.
|
||||||
|
3. Avoid global overrides, unless it is core crate issue, a good example of this is the Bevy Engine that has a set of lints that should be allowed by default.
|
||||||
|
|
||||||
|
## 2.5 Configure workspace/package lints
|
||||||
|
|
||||||
|
In your `Cargo.toml` file it is possible to determine which lints and their priorities over each other. In case of 2 or more conflicting lints, the higher priority one will be chosen. Example configuration for a package:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[lints.rust]
|
||||||
|
future-incompatible = "warn"
|
||||||
|
nonstandard_style = "deny"
|
||||||
|
|
||||||
|
[lints.clippy]
|
||||||
|
all = { level = "deny", priority = 10 }
|
||||||
|
redundant_clone = { level = "deny", priority = 9 }
|
||||||
|
manual_while_let_some = { level = "deny", priority = 4 }
|
||||||
|
pedantic = { level = "warn", priority = 3 }
|
||||||
|
```
|
||||||
|
|
||||||
|
And for a workspace:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[workspace.lints.rust]
|
||||||
|
future-incompatible = "warn"
|
||||||
|
nonstandard_style = "deny"
|
||||||
|
|
||||||
|
[workspace.lints.clippy]
|
||||||
|
all = { level = "deny", priority = 10 }
|
||||||
|
redundant_clone = { level = "deny", priority = 9 }
|
||||||
|
manual_while_let_some = { level = "deny", priority = 4 }
|
||||||
|
pedantic = { level = "warn", priority = 3 }
|
||||||
|
```
|
||||||
213
skills/rust-best-practices/references/chapter_03.md
Normal file
213
skills/rust-best-practices/references/chapter_03.md
Normal file
@@ -0,0 +1,213 @@
|
|||||||
|
# Chapter 3 - Performance Mindset
|
||||||
|
|
||||||
|
The **golden rule** of performance work:
|
||||||
|
|
||||||
|
> Don't guess, measure.
|
||||||
|
|
||||||
|
Rust code is often already pretty fast - don't "optimize" without evidence. Optimize only after finding bottlenecks.
|
||||||
|
|
||||||
|
### A good first steps
|
||||||
|
* Use `--release` flag on your builds (might sound dummy, but it is quite common to hear people complaining that their Rust code is slower than their X language code, and 99% of the time is because they didn't use the `--release` flag).
|
||||||
|
* `$ cargo clippy -- -D clippy::perf` gives you important tips on best practices for performance.
|
||||||
|
* [`cargo bench`](https://doc.rust-lang.org/cargo/commands/cargo-bench.html) is a cargo tool to create micro-benchmarks and test different code solutions. Write a test scenario and bench your solution against the original code, if your improvement is larger than 5%, might be a good performance improvement.
|
||||||
|
* [`cargo flamegraph`](https://github.com/flamegraph-rs/flamegraph) a powerful profiler for Rust code. For MacOS, [samply](https://github.com/mstange/samply) might be a better DX option.
|
||||||
|
|
||||||
|
> #### Further reading on Benchmarking:
|
||||||
|
> - [How to build a Custom Benchmarking Harness in Rust](https://bencher.dev/learn/benchmarking/rust/custom-harness/)
|
||||||
|
|
||||||
|
|
||||||
|
## 3.1 Flamegraph
|
||||||
|
|
||||||
|
Flamegraph helps you visualize how much time CPU spent on each task.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
# Installing flamegraph
|
||||||
|
cargo install flamegraph
|
||||||
|
|
||||||
|
# cargo support provided through the cargo-flamegraph binary!
|
||||||
|
# defaults to profiling cargo run --release
|
||||||
|
cargo flamegraph
|
||||||
|
|
||||||
|
# by default, `--release` profile is used,
|
||||||
|
# but you can override this:
|
||||||
|
cargo flamegraph --dev
|
||||||
|
|
||||||
|
# if you'd like to profile a specific binary:
|
||||||
|
cargo flamegraph --bin=stress2
|
||||||
|
|
||||||
|
# Profile unit tests.
|
||||||
|
# Note that a separating `--` is necessary if `--unit-test` is the last flag.
|
||||||
|
cargo flamegraph --unit-test -- test::in::package::with::single::crate
|
||||||
|
cargo flamegraph --unit-test crate_name -- test::in::package::with::multiple:crate
|
||||||
|
|
||||||
|
# Profile integration tests.
|
||||||
|
cargo flamegraph --test test_name
|
||||||
|
|
||||||
|
# Run criterion benchmark
|
||||||
|
# Note that the last --bench is required for `criterion 0.3` to run in benchmark mode, instead of test mode.
|
||||||
|
cargo flamegraph --bench some_benchmark --features some_features -- --bench
|
||||||
|
|
||||||
|
# Run workspace example
|
||||||
|
cargo flamegraph --example some_example --features some_features
|
||||||
|
```
|
||||||
|
|
||||||
|
> ❗ Always run your profiles with `--release` enabled, the `--dev` flag isn't realistic as it doesn't have optimizations enabled.
|
||||||
|
|
||||||
|
The result will look like the following:
|
||||||
|
<img src="/wiki/download/attachments/1532592375/flamegraph.png" width="1000px" height="377px" onerror="this.style.display='none'"/>
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
* The `y-axis` shows the **stack depth number**. When looking at a flamegraph, the main function of your program will be closer to the bottom, and the called functions will be stacked on top, with the functions that they call stacked on top of them.
|
||||||
|
|
||||||
|
The `width of each box` shows the **total time that that function** is on the CPU or is part of the call stack. If a function's box is wider than others, that means that it consumes more CPU per execution than other functions, or that it is called more than other functions.
|
||||||
|
|
||||||
|
> ❗ The **color of each box** isn't significant, and **is chosen at random**.
|
||||||
|
|
||||||
|
### 🚨 Remember
|
||||||
|
* Thick stacks: heavy CPU usage
|
||||||
|
* Thin stacks: low intensity (cheap)
|
||||||
|
|
||||||
|
## 3.2 Avoid Redundant Cloning
|
||||||
|
|
||||||
|
> Cloning is cheap... **until it isn't**
|
||||||
|
|
||||||
|
In sections [Borrowing over Cloning](./chapter_01.md#11-borrowing-over-cloning) and [Important Clippy lints to respect](./chapter_02.md#23-important-clippy-lints-to-respect) we mentioned the impacts of cloning and the relevant clippy lint [`redundant_clone`](https://rust-lang.github.io/rust-clippy/master/#redundant_clone), so in this section we will explore a bit "when to pass ownership".
|
||||||
|
|
||||||
|
* 🚨 If you really need to clone, leave it to the last moment.
|
||||||
|
|
||||||
|
### When to pass ownership?
|
||||||
|
|
||||||
|
* Only `.clone()` if you truly need a new owned copy. A few examples:
|
||||||
|
* Crate API Design requires owned data.
|
||||||
|
* Have overloaded `std::ops` but still need ownership to the old data:
|
||||||
|
```rust
|
||||||
|
use std::ops::Add;
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
|
struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for Point {
|
||||||
|
type Output = Self;
|
||||||
|
|
||||||
|
fn add(self, other: Self) -> Self {
|
||||||
|
Self {
|
||||||
|
x: self.x + other.x,
|
||||||
|
y: self.y + other.y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
|
||||||
|
Point { x: 3, y: 3 });
|
||||||
|
```
|
||||||
|
* Need to do comparison snapshots or due to API you need multiple owned instances of the data.
|
||||||
|
```rust
|
||||||
|
fn snapshot(a: &MyValue, b:&MyValue) -> MyValueDiff {
|
||||||
|
a - b
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub for MyValue {
|
||||||
|
type Output = MyValueDiff;
|
||||||
|
|
||||||
|
fn sub(self, other: Self) -> MyValue {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut a = MyValue::default();
|
||||||
|
let b = a.clone();
|
||||||
|
|
||||||
|
a.magical_update();
|
||||||
|
println!("{:?}", snapshot(&a, &b));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* You have reference counted pointers (`Arc, Rc`).
|
||||||
|
* You have small structs that are too big to `Copy` but as costly as `std::collections`. An example is HTTP client like `hyper_util::client::legacy::Client` that cloning allows you to share the connection pool.
|
||||||
|
* You have a chained struct modifier that needs owned mutation, some **builders** require owned mutation, but most custom builders can be done with `pub fn with_xyz(&mut self, value: Xyz) -> &mut Self`.
|
||||||
|
```rust
|
||||||
|
// Inline `HashMap` insertion extension
|
||||||
|
|
||||||
|
fn insert_owned(mut self, key: K, value: V) -> Self {
|
||||||
|
self.insert(key, value);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
* Ownership can also be a good way to model business logic / state. For example:
|
||||||
|
```rust
|
||||||
|
let not_validated: String = ...;// some user source
|
||||||
|
let validated = Validate::try_from(not_validated)?;
|
||||||
|
// Technically that `try_from` maybe didn't need ownership, but taking it lets us model intent
|
||||||
|
```
|
||||||
|
|
||||||
|
### When **NOT** to pass ownership?
|
||||||
|
|
||||||
|
* Prefer API designs that take reference (`fn process(values: &[T])`), instead of ownership (`fn process(values: Vec<T>)`).
|
||||||
|
* If you only need read access to elements, prefer `.iter` or slices:
|
||||||
|
```rust
|
||||||
|
for item in &some_vec {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* You need to mutate data that is owned by another thread, use `&mut MyStruct`.
|
||||||
|
|
||||||
|
### Use `Cow` for `Maybe Owned` data
|
||||||
|
|
||||||
|
Sometimes you don't actually need owned data, but that is not clear from the API perspective, so using [`std::borrow::Cow`](https://doc.rust-lang.org/std/borrow/enum.Cow.html) is a way to efficiently address this case:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
fn hello_greet(name: Cow<'_, str>) {
|
||||||
|
println!("Hello {name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
hello_greet(Cow::Borrowed("Julia"));
|
||||||
|
hello_greet(Cow::Owned("Naomi".to_string()));
|
||||||
|
```
|
||||||
|
|
||||||
|
## 3.3 Stack vs Heap: Be size-smart!
|
||||||
|
|
||||||
|
### ✅ Good Practices
|
||||||
|
|
||||||
|
* Keep small types (`impl Copy`, `usize`, `bool`, etc) **on the stack**.
|
||||||
|
* Avoid passing huge types (`> 512 bytes`) by value or transferring ownership. Prefer pass by reference (e.g. `&T` and `&mut T`).
|
||||||
|
* Heap allocate recursive data structures:
|
||||||
|
```rust
|
||||||
|
enum OctreeNode<T> {
|
||||||
|
Node(T),
|
||||||
|
Children(Box<[Node<T>; 8]>),
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* Return small types by value, types that implement `Copy` or a cheaply Cloned are efficient to return by value (e.g. `struct Vector2 {x: f32, y: f32}`).
|
||||||
|
|
||||||
|
### ❗ Be Mindful
|
||||||
|
|
||||||
|
* Only use `#[inline]` when benchmark proves beneficial, Rust is already pretty good at inlining **without** hints.
|
||||||
|
* Avoid massive stack allocations, box them. Example `let buffer: Box<[u8; 65536]> = Box::new(..)` would first allocate `[u8; 65536]` on the stack then box it, a non-const solution to this would be `let buffer: Box<[u8]> = vec![0; 65536].into_boxed_slice()`.
|
||||||
|
* For large `const` arrays, considering using [crate smallvec](https://docs.rs/smallvec/latest/smallvec/) as it behaves like an array, but is smart enough to allocate large arrays on the heap.
|
||||||
|
|
||||||
|
## 3.4 Iterators and Zero-Cost Abstractions
|
||||||
|
|
||||||
|
Rust iterators are lazy, but eventually compiled away into very efficient tight loops that are only called when consumed. Chaining `.filter()`, `.map()`, `.rev()`, `.skip()`, `.take()`, `.collect()` usually doesn't cost extra and the compiler can reason well enough how to optimize them.
|
||||||
|
* Prefer `iterators` over manual `for` loops when working with collections, the compiler can optimize them better than manually doing it.
|
||||||
|
* Calling `.iter()` only creates a **reference** to the original collection, this allows you to hold multiple iterators of the same collection.
|
||||||
|
|
||||||
|
#### ❗ Avoid creating intermediate collections unless it is really needed:
|
||||||
|
|
||||||
|
* Consider that `process` accepts an `iterator`.
|
||||||
|
* ❌ BAD - useless intermediate collection:
|
||||||
|
```rust
|
||||||
|
let doubled: Vec<_> = items.iter().map(|x| x * 2).collect();
|
||||||
|
process(doubled);
|
||||||
|
```
|
||||||
|
* ✅ GOOD - pass the iterator (`fn process(arg: impl Iterator<Item = u32>)`):
|
||||||
|
```rust
|
||||||
|
let doubled_iter = items.iter().map(|x| x * 2);
|
||||||
|
process(doubled_iter);
|
||||||
|
```
|
||||||
181
skills/rust-best-practices/references/chapter_04.md
Normal file
181
skills/rust-best-practices/references/chapter_04.md
Normal file
@@ -0,0 +1,181 @@
|
|||||||
|
# Chapter 4 - Errors Handling
|
||||||
|
|
||||||
|
Rust enforces a strict error handling approach, but *how* you handle them defines where your code feels ergonomic, consistent and safe - as opposed to cryptic and painful. This chapter dives into best practices for modeling and managing fallible operations across libraries and binaries.
|
||||||
|
|
||||||
|
> Even if you decide to crash your application with `unwrap` or `expect`, Rust forces you to declare that intentionally.
|
||||||
|
|
||||||
|
## 4.1 Prefer `Result`, avoid panic 🫨
|
||||||
|
|
||||||
|
Rust has a powerful type that wraps fallible data, [`Result`](https://doc.rust-lang.org/std/result/), this allows us to handle Error cases according to our needs and manage the state of the application based on that.
|
||||||
|
|
||||||
|
* If your function can fail, prefer to return a `Result`:
|
||||||
|
```rust
|
||||||
|
fn divide(x: f64, y: f64) -> Result<f64, DivisionError> {
|
||||||
|
if y == 0.0 {
|
||||||
|
Err(DivisionError::DividedByZero)
|
||||||
|
} else {
|
||||||
|
Ok(x / y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Use `panic!` only in unrecoverable conditions - typically tests, assertions, bugs or a need to crash the application for some explicit reason.
|
||||||
|
* There are 3 relevant macros that can replace `panic!` in appropriate conditions:
|
||||||
|
* `todo!`, similar to panic, but alerts the compiler that you are aware that there is code missing.
|
||||||
|
* `unreachable!`, you have reasoned about the code block and are sure that condition `xyz` is not possible and if ever becomes possible you want to be alerted.
|
||||||
|
* `unimplemented!`, specially useful for alerting that a block is not yet implemented with a reason.
|
||||||
|
|
||||||
|
## 4.2 Avoid `unwrap`/`expect` in Production
|
||||||
|
|
||||||
|
Although `expect` is preferred to `unwrap`, as it can have context, they should be avoided in production code as there are smarter alternatives to them. Considering that, they should be used in the following scenarios:
|
||||||
|
- In tests, assertions or test helper functions.
|
||||||
|
- When failure is impossible.
|
||||||
|
- When the smarter options can't handle the specific case.
|
||||||
|
|
||||||
|
### 🚨 Alternative ways of handling `unwrap`/`expect`:
|
||||||
|
|
||||||
|
* If your `Result` (or `Option`) can have a predefined early return value in case of `Result::Err`, that doesn't need to know the `Err` value, use `let Ok(..) = else { return ... }` pattern, as it helps with flatten functions:
|
||||||
|
```rust
|
||||||
|
let Ok(json) = serde_json::from_str(&input) else {
|
||||||
|
return Err(MyError::InvalidJson);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* If your `Result` (or `Option`) needs error recovery in case of `Result::Err`, that doesn't need to know the `Err` value, use `if let Ok(..) else { ... }` pattern:
|
||||||
|
```rust
|
||||||
|
if let Ok(json) = serde_json::from_str(&input) else {
|
||||||
|
...
|
||||||
|
} else {
|
||||||
|
Err(do_something_with_input(&input))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* Functions that can have to handle `Option::None` values are recommended to return `Result<T, E>`, where `E` is a crate or module level error, like the examples above.
|
||||||
|
* Lastly `unwrap_or`, `unwrap_or_else` or `unwrap_or_default`, these functions help you create alternative exits to unwrap that manage the uninitialized values.
|
||||||
|
|
||||||
|
## 4.3 `thiserror` for Crate level errors
|
||||||
|
|
||||||
|
Deriving Error manually is verbose and error prone, the rust ecosystem has a really good crate to help with this, `thiserror`. It allows you to create error types that easily implement `From` trait as well as easy error message (`Display`), improving developer experience while working seamlessly with `?` and integrating with `std::error::Error`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum MyError {
|
||||||
|
#[error("Network Timeout")]
|
||||||
|
Timeout,
|
||||||
|
#[error("Invalid data: {0}")]
|
||||||
|
InvalidData(String),
|
||||||
|
#[error(transparent)]
|
||||||
|
Serialization(#[from] serde_json::Error),
|
||||||
|
#[error("Invalid request information. Header: {headers}, Metadata: {metadata}")]
|
||||||
|
InvalidRequest {
|
||||||
|
headers: Headers,
|
||||||
|
metadata: Metadata
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Error Hierarchies and Wrapping
|
||||||
|
|
||||||
|
For layered systems the best practice is to use nested `enum/struct` errors with `#[from]`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use crate::database::DbError;
|
||||||
|
use crate::external_services::ExternalHttpError;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum ServiceError {
|
||||||
|
#[error("Database handler error: {0}")]
|
||||||
|
Db(#[from] DbError),
|
||||||
|
#[error("External services error: {0}")]
|
||||||
|
ExternalServices(#[from] ExternalHttpError)
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4.4 Reserve `anyhow` for Binaries
|
||||||
|
|
||||||
|
`anyhow` is an amazing crate, and quite useful for projects that are beginning and need accelerated speed. However, there is a turning point where it just painfully propagates through your code, considering this, `anyhow` is recommended only for **binaries**, where ergonomic error handling is needed and there is no need for precise error types:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use anyhow::{Context, Result, anyhow};
|
||||||
|
|
||||||
|
fn main() -> Result<Config> {
|
||||||
|
let content = std::fs::read_to_string("config.json")
|
||||||
|
.context("Failed to read config file")?;
|
||||||
|
Config::from_str(&content)
|
||||||
|
.map_err(|err| anyhow!("Config parsing error: {err}"))
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 🚨 `Anyhow` Gotchas
|
||||||
|
|
||||||
|
* Keeping the `context` and `anyhow` strings up-to-date in all code base is harder than keeping `thiserror` messages as you don't have a single point of entry.
|
||||||
|
* `anyhow::Result` erases context that a caller might need, so avoid using it in a library.
|
||||||
|
* test helper functions can use `anyhow` with little to no issues.
|
||||||
|
|
||||||
|
## 4.5 Use `?` to Bubble Errors
|
||||||
|
|
||||||
|
Prefer using `?` over verbose alternatives like `match` chains:
|
||||||
|
```rust
|
||||||
|
fn handle_request(req: &Request) -> Result<ValidatedRequest, RequestValidationError> {
|
||||||
|
validate_headers(req)?;
|
||||||
|
validate_body_format(req)?;
|
||||||
|
validate_credentials(req)?;
|
||||||
|
let body = Body::try_from(req)?;
|
||||||
|
|
||||||
|
Ok(ValidatedRequest::try_from((req, body))?)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> In case error recovery is needed, use `or_else`, `map_err`, `if let Ok(..) else`. To **inspect or log your error**, use `inspect_err`.
|
||||||
|
|
||||||
|
## 4.6 Unit Test should exercise errors
|
||||||
|
|
||||||
|
While many errors don't implement PartialEq and Eq, making it hard to do direct assertions between them, it is possible to check the error messages with `format!` or `to_string()`, making the errors meaningful and test validated:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[test]
|
||||||
|
fn error_does_not_implement_partial_eq() {
|
||||||
|
let err = divide(10., 0.0).unwrap_err();
|
||||||
|
assert_eq!(err.to_string(), "division by zero");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn error_implements_partial_eq() {
|
||||||
|
let err = process(my_value).unwrap_err();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
err,
|
||||||
|
MyError {
|
||||||
|
..
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 4.7 Important Topics
|
||||||
|
|
||||||
|
### Custom Error Structs
|
||||||
|
|
||||||
|
Sometimes you don't need an enum to handle your errors, as there is only one type of error that your module can have. This can be solved with `struct Errors`:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[derive(Debug, thiserror::Error, PartialEq)]
|
||||||
|
#[error("Request failed with code `{code}`: {message}")]
|
||||||
|
struct HttpError {
|
||||||
|
code: u16,
|
||||||
|
message: String
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Async Errors
|
||||||
|
|
||||||
|
When using async runtimes, like Tokio, make sure that your errors implement `Send + Sync + 'static` where needed, specially in tasks or across `.await` boundaries:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
...
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> Avoid `Box<dyn std::error::Error>` in libraries unless it is really needed
|
||||||
392
skills/rust-best-practices/references/chapter_05.md
Normal file
392
skills/rust-best-practices/references/chapter_05.md
Normal file
@@ -0,0 +1,392 @@
|
|||||||
|
# Chapter 5 - Automated Testing
|
||||||
|
|
||||||
|
> Tests are not just for correctness. They are the first place people look to understand how your code works.
|
||||||
|
|
||||||
|
* Tests in rust are declared with the attribute macro `#[test]`. Most code editors can compile and run the functions declared under the macro individually or blocks of them.
|
||||||
|
* Test can have special compilation flags with `#[cfg(test)]`. Also executable in code editors if it contained `#[test]`, it is a good way to mock complicated functions or override traits.
|
||||||
|
|
||||||
|
## 5.1 Tests as Living Documentation
|
||||||
|
|
||||||
|
In Rust, as in many other languages, tests often show how the functions are meant to be used. If a test is clear and targeted, it's often more helpful than reading the function body, when combined with other tests, they serve as living documentation.
|
||||||
|
|
||||||
|
### Use descriptive names
|
||||||
|
|
||||||
|
> In the unit test name we should see the following:
|
||||||
|
> * `unit_of_work`: which *function* we are calling. The **action** that will be executed. This is often the name of the test `mod` where the function is being tested.
|
||||||
|
```rust
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
mod function_name {
|
||||||
|
#[test]
|
||||||
|
fn returns_y_when_x() { ... }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
> * `expected_behavior`: the set of **assertions** that we need to verify that the test works.
|
||||||
|
> * `state_that_the_test_will_check`: the general **arrangement**, or setup, of the specific test case.
|
||||||
|
|
||||||
|
#### ❌ Don't use a generic name for a test
|
||||||
|
```rust
|
||||||
|
#[test]
|
||||||
|
fn test_add_happy_path() {
|
||||||
|
assert_eq!(add(2, 2), 4);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
#### ✅ Use a name which reads like a sentence, describing the desired behavior
|
||||||
|
> Alternatively, if your function has too many tests, you can blob them together in a `mod`, it makes it easier to read and navigate.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// OPTION 1
|
||||||
|
#[test]
|
||||||
|
fn process_should_return_blob_when_larger_than_b() {
|
||||||
|
let a = setup_a_to_be_xyz();
|
||||||
|
let b = Some(2);
|
||||||
|
let expected = MyExpectedStruct { ... };
|
||||||
|
|
||||||
|
let result = process(a, b).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(result, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
// OPTION 2
|
||||||
|
mod process {
|
||||||
|
#[test]
|
||||||
|
fn should_return_blob_when_larger_than_b() {
|
||||||
|
let a = setup_a_to_be_xyz();
|
||||||
|
let b = Some(2);
|
||||||
|
let expected = MyExpectedStruct { ... };
|
||||||
|
|
||||||
|
let result = process(a, b).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(result, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> When executing `cargo test` the test output for each option will look like:
|
||||||
|
> Option 1: `process_should_return_blob_when_larger_than_b`.
|
||||||
|
> Option 2: `process::should_return_blob_when_larger_than_b`.
|
||||||
|
|
||||||
|
### Use modules for organization
|
||||||
|
|
||||||
|
Most IDEs can run a single module of tests all together.
|
||||||
|
The test name in the output will also contain the name of the module.
|
||||||
|
Together, that means you can use the module name to group related tests together:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test { // IDEs will provide a ▶️ button here
|
||||||
|
|
||||||
|
mod process {
|
||||||
|
#[test] // IDEs will provide a ▶️ button here
|
||||||
|
fn returns_error_xyz_when_b_is_negative() {
|
||||||
|
let a = setup_a_to_be_xyz();
|
||||||
|
let b = Some(-5);
|
||||||
|
let expected = MyError::Xyz;
|
||||||
|
|
||||||
|
let result = process(a, b).unwrap_err();
|
||||||
|
|
||||||
|
assert_eq!(result, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test] // IDEs will provide a ▶️ button here
|
||||||
|
fn returns_invalid_input_error_when_a_and_b_not_present() {
|
||||||
|
let a = None;
|
||||||
|
let b = None;
|
||||||
|
let expected = MyError::InvalidInput;
|
||||||
|
|
||||||
|
let result = process(a, b).unwrap_err();
|
||||||
|
|
||||||
|
assert_eq!(result, expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Only test one behavior per function
|
||||||
|
|
||||||
|
To keep tests clear, they should describe _one_ thing that the unit does.
|
||||||
|
This makes it easier to understand why a test is failing.
|
||||||
|
|
||||||
|
#### ❌ Don't test multiple things in the same test
|
||||||
|
```rust
|
||||||
|
fn test_thing_parser(...) {
|
||||||
|
assert!(Thing::parse("abcd").is_ok());
|
||||||
|
assert!(Thing::parse("ABCD").is_err());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### ✅ Test one thing per test
|
||||||
|
```rust
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test_thing_parser {
|
||||||
|
#[test]
|
||||||
|
fn lowercase_letters_are_valid() {
|
||||||
|
assert!(
|
||||||
|
Thing::parse("abcd").is_ok(),
|
||||||
|
// Works like `eprintln`, `format` and `println` macros
|
||||||
|
"Thing parse error: {:?}",
|
||||||
|
Thing::parse("abcd").unwrap_err()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn capital_letters_are_invalid() {
|
||||||
|
assert!(Thing::parse("ABCD").is_err());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> `Ok` scenarios should have an `eprintln` of the `Err` case.
|
||||||
|
|
||||||
|
### Use very few, ideally one, assertion per test
|
||||||
|
|
||||||
|
When there are multiple assertions per test, it's both harder to understand the intended behavior and
|
||||||
|
often requires many iterations to fix a broken test, as you work through assertions one by one.
|
||||||
|
|
||||||
|
❌ Don't include many assertions in one test:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[test]
|
||||||
|
fn test_valid_inputs() {
|
||||||
|
assert!(the_function("a").is_ok());
|
||||||
|
assert!(the_function("ab").is_ok());
|
||||||
|
assert!(the_function("ba").is_ok());
|
||||||
|
assert!(the_function("bab").is_ok());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
If you are testing separate behaviors, make multiple tests each with descriptive names.
|
||||||
|
To avoid boilerplate, either use a shared setup function or [rstest](https://crates.io/crates/rstest) cases *with descriptive test names*:
|
||||||
|
```rust
|
||||||
|
#[rstest]
|
||||||
|
#[case::single("a")]
|
||||||
|
#[case::first_letter("ab")]
|
||||||
|
#[case::last_letter("ba")]
|
||||||
|
#[case::in_the_middle("bab")]
|
||||||
|
fn the_function_accepts_all_strings_with_a(#[case] input: &str) {
|
||||||
|
assert!(the_function(input).is_ok());
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> Considerations when using `rstest`
|
||||||
|
>
|
||||||
|
> * It’s harder for both IDEs and humans to run/locate specific tests.
|
||||||
|
> * Expectation vs condition naming is now visually inverted (expectation first).
|
||||||
|
|
||||||
|
> ❗ Share **setup**, not the test itself: keep each test's action and assertion inline, even when repetitive. Tests tolerate duplication better than production code — see [Chapter 1, §1.8](chapter_01.md#-test-code-readability-beats-dry).
|
||||||
|
|
||||||
|
## 5.2 Add Test Examples to your Docs
|
||||||
|
|
||||||
|
We will deep dive into docs at a later stage, so in this section we will just briefly go over how to add tests to your docs. Rustdoc can turn examples into executable tests using `///` with a few advantages:
|
||||||
|
|
||||||
|
* These tests run with `cargo test` **BUT NOT** `cargo nextest run`. If using `nextest`, make sure to run `cargo t --doc` separately.
|
||||||
|
* They serve both as documentation and correctness checks, and are kept up to date by changes, due to the fact that the compiler checks them.
|
||||||
|
* No extra testing boilerplate. You can easily hide test sections by prefixing the line with `#`.
|
||||||
|
* ❗ There is no issue if you have test duplication between doc-tests and other non-public facing tests.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
/// Helper function that adds any two numeric values together.
|
||||||
|
/// This function reasons about which would be the correct type to parse based on the type
|
||||||
|
/// and the size of the numeric value.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use crate_name::generic_add;
|
||||||
|
/// use num::numeric;
|
||||||
|
///
|
||||||
|
/// # assert_eq!(
|
||||||
|
/// generic_add(5.2, 4) // => 9.2
|
||||||
|
/// # , 9.2)
|
||||||
|
///
|
||||||
|
/// # assert_eq!(
|
||||||
|
/// generic_add(2, 2.0) // => 4
|
||||||
|
/// # , 4)
|
||||||
|
/// ```
|
||||||
|
```
|
||||||
|
|
||||||
|
This documentation code would look like:
|
||||||
|
```rust
|
||||||
|
use num::numeric;
|
||||||
|
|
||||||
|
generic_add(5.2, 4) // => 9.2
|
||||||
|
generic_add(2, 2.0) // => 4
|
||||||
|
```
|
||||||
|
|
||||||
|
## 5.3 Unit Test vs Integration Tests vs Doc tests
|
||||||
|
|
||||||
|
As a general rule, without delving into *test pyramid naming*, rust has 3 sets of tests:
|
||||||
|
|
||||||
|
### Unit Test
|
||||||
|
|
||||||
|
Tests that go in the **same module** as the **tested unit** was declared, this allows the test runner to have visibility over private functions and parent `use` declarations. They can also consume `pub(crate)` functions from other modules if needed. Unit tests can be more focused on **implementation and edge-cases checks**.
|
||||||
|
|
||||||
|
* They should be as simple as possible, testing one state and one behavior of the unit. KISS.
|
||||||
|
* They should test for errors and edge cases.
|
||||||
|
* Different tests of the same unit can be combined under a single `#[cfg(test)] mod test_unit_of_work {...}`, allowing multiple submodules for different `units_of_work`.
|
||||||
|
* Try to keep external states/side effects to your API to minimum and focus those tests on the `mod.rs` files.
|
||||||
|
* Tests that are not yet fully implemented can be ignored with the `#[ignore = "optional message"]` attribute.
|
||||||
|
* Tests that intentionally panic should be annotated with the attribute `#[should_panic]`.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#[cfg(test)]
|
||||||
|
mod unit_of_work_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unit_state_behavior() {
|
||||||
|
let expected = ...;
|
||||||
|
let result = ...;
|
||||||
|
assert_eq!(result, expected, "Failed because {}", result - expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Integration Tests
|
||||||
|
|
||||||
|
Tests that go under the `tests/` directory, they are entirely external to your library and use the same code as any other code would use, not have access to private and crate level functions, which means they can **only test** functions on your **public API**.
|
||||||
|
|
||||||
|
> Their purpose is to test whether many parts of the code work together correctly, units of code that work correctly on their own could have problems when integrated.
|
||||||
|
|
||||||
|
* Test for happy paths and common use cases.
|
||||||
|
* Allow external states and side effects, [testcontainers](https://rust.testcontainers.org/) might help.
|
||||||
|
* if testing binaries, try to break **executable** and **functions** into `src/main.rs` and `src/lib.rs`, respectively.
|
||||||
|
|
||||||
|
```
|
||||||
|
├── Cargo.lock
|
||||||
|
├── Cargo.toml
|
||||||
|
├── src
|
||||||
|
│ └── lib.rs
|
||||||
|
└── tests
|
||||||
|
├── mod.rs
|
||||||
|
├── common
|
||||||
|
│ └── mod.rs
|
||||||
|
└── integration_test.rs
|
||||||
|
```
|
||||||
|
|
||||||
|
### Doc Testing
|
||||||
|
|
||||||
|
As mentioned in section [5.2](#52-add-test-examples-to-your-docs), doc tests should have happy paths, general public API usage and more powerful attributes that improve documentation, like custom CSS for the code blocks.
|
||||||
|
|
||||||
|
### Attributes:
|
||||||
|
|
||||||
|
* `ignore`: tells rust to ignore the code, usually not recommended, if you want just a code formatted text, use `text`.
|
||||||
|
* `should_panic`: tells the rust compiler that this example block will panic.
|
||||||
|
* `no_run`: compiles but doesn't execute the code, similar to `cargo check`. Very useful when dealing with side-effects for documentation.
|
||||||
|
* `compile_fail`: Test rustdoc that this block should cause a compilation fail, important when you want to demonstrate wrong use cases.
|
||||||
|
|
||||||
|
## 5.4 How to `assert!`
|
||||||
|
|
||||||
|
Rust comes with 2 macros to make assertions:
|
||||||
|
* `assert!` for asserting boolean values like `assert!(value.is_ok(), "'value' is not Ok: {value:?}")`
|
||||||
|
* `assert_eq!` for checking equality between two different values, `assert_eq!(result, expected, "'result' differs from 'expected': {}", result.diff(expected))`.
|
||||||
|
|
||||||
|
### 🚨 `assert!` reminders
|
||||||
|
* Rust asserts support formatted strings, like the previous examples, those strings will be printed in case of failure, so it is a good practice to add what the actual state was and how it differs from the expected.
|
||||||
|
* If you don't care about the exact pattern matching value, using `matches!` combined with `assert!` might be a good alternative. Note the message string is an argument to `assert!`, not to `matches!`:
|
||||||
|
```rust
|
||||||
|
assert!(matches!(error, MyError::BadInput(_)), "Expected `BadInput`, found {error}");
|
||||||
|
```
|
||||||
|
* Since Rust 1.96 prefer the stabilized [`assert_matches!`](https://doc.rust-lang.org/std/macro.assert_matches.html) (and [`debug_assert_matches!`](https://doc.rust-lang.org/std/macro.debug_assert_matches.html)) over `assert!(matches!(..))`. It prints the actual value on failure for free, so you don't have to write the message yourself:
|
||||||
|
```rust
|
||||||
|
use std::assert_matches::assert_matches;
|
||||||
|
|
||||||
|
assert_matches!(error, MyError::BadInput(_));
|
||||||
|
```
|
||||||
|
* Use `#[should_panic]` wisely. It should only be used when panic is the desired behavior, prefer result instead of panic.
|
||||||
|
* There are some other that can enhance your testing experience like:
|
||||||
|
* [`rstest`](https://crates.io/crates/rstest): fixture based test framework with procedural macros.
|
||||||
|
* [`pretty_assertions`](https://crates.io/crates/pretty_assertions): overrides `assert_eq` and `assert_ne`, and creates colorful diffs between them.
|
||||||
|
|
||||||
|
## 5.5 Snapshot Testing with `cargo insta`
|
||||||
|
|
||||||
|
> When correctness is visual or structural, snapshots tell the story better than asserts.
|
||||||
|
|
||||||
|
1. Add to your dependencies:
|
||||||
|
```toml
|
||||||
|
insta = { version = "1.42.2", features = ["yaml"] }
|
||||||
|
```
|
||||||
|
> For most real world applications the recommendation is to use YAML snapshots of serializable values. This is because they look best under version control and the diff viewer and support redaction. To use this enable the yaml feature of insta.
|
||||||
|
|
||||||
|
2. For a better review experience, add the CLI `cargo install cargo-insta`.
|
||||||
|
<img src="/wiki/download/attachments/1532592393/insta.svg" onerror="this.style.display='none'"/>
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
3. Writing a simple test:
|
||||||
|
```rust
|
||||||
|
fn split_words(s: &str) -> Vec<&str> {
|
||||||
|
s.split_whitespace().collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_split_words() {
|
||||||
|
let words = split_words("hello from the other side");
|
||||||
|
insta::assert_yaml_snapshot!(words);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Run `cargo insta test` to execute, and `cargo insta review` to review conflicts.
|
||||||
|
|
||||||
|
To learn more about `cargo insta` check out its [documentation](https://insta.rs/docs/quickstart/) as it is a very complete and well documented tool.
|
||||||
|
|
||||||
|
### What is snapshot testing?
|
||||||
|
|
||||||
|
Snapshot testing compares your output (text, Json, HTML, YAML, etc) against a saved "golden" version. On future runs, the test fails if the output changes, unless humanly approved. It is perfect for:
|
||||||
|
* Generate code.
|
||||||
|
* Serializing complex data.
|
||||||
|
* Rendered HTML.
|
||||||
|
* CLI output.
|
||||||
|
|
||||||
|
#### ❌ What not to test with snapshot
|
||||||
|
* Very stable, numeric-only or small structured data associated logic (prefer `assert_eq!`).
|
||||||
|
* Critical path logic (prefer precise unit tests).
|
||||||
|
* Flaky tests, randomly generated output, unless redacted.
|
||||||
|
* Snapshots of external resources, use mocks and stubs.
|
||||||
|
|
||||||
|
## 5.6 ✅ Snapshot Best Practices
|
||||||
|
|
||||||
|
* Named snapshots, it gives meaningful snapshot files names, e.g. `snapshots/this_is_a_named_snapshot.snap`
|
||||||
|
```rust
|
||||||
|
assert_snapshot!("this_is_a_named_snapshot", output);
|
||||||
|
```
|
||||||
|
|
||||||
|
* Keep snapshots small and clear.
|
||||||
|
```rust
|
||||||
|
// ✅ Best case:
|
||||||
|
assert_snapshot!("app_config/http", whole_app_config.http);
|
||||||
|
|
||||||
|
// ❌ Worst case:
|
||||||
|
assert_snapshot!("app_config", whole_app_config); // Huge object
|
||||||
|
```
|
||||||
|
|
||||||
|
> #### 🚨 Avoid snapshotting huge objects
|
||||||
|
> Huge objects become hard to review and reason about.
|
||||||
|
|
||||||
|
* Avoid snapshotting simple types (primitives, flat enums, small structs):
|
||||||
|
```rust
|
||||||
|
// ✅ Better:
|
||||||
|
assert_eq!(meaning_of_life, 42);
|
||||||
|
|
||||||
|
// ❌ OVERKILL:
|
||||||
|
assert_snapshot!("the_meaning_of_life", meaning_of_life); // meaning_of_life == 42
|
||||||
|
```
|
||||||
|
|
||||||
|
* Use [redactions](https://insta.rs/docs/redactions/) for unstable fields (randomly generated, timestamps, uuid, etc):
|
||||||
|
```rust
|
||||||
|
use insta::assert_json_snapshot;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn endpoint_get_user_data() {
|
||||||
|
let data = http::client.get_user_data();
|
||||||
|
assert_json_snapshot!(
|
||||||
|
"endpoints/subroute/get_user_data",
|
||||||
|
data,
|
||||||
|
".created_at" => "[timestamp]",
|
||||||
|
".id" => "[uuid]"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* Commit your snapshots into git. They will be stored in `snapshots/` alongside your tests.
|
||||||
|
* Review changes carefully before accepting.
|
||||||
161
skills/rust-best-practices/references/chapter_06.md
Normal file
161
skills/rust-best-practices/references/chapter_06.md
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
# Chapter 6 - Generics, Dynamic Dispatch and Static Dispatch
|
||||||
|
|
||||||
|
> Static where you can, dynamic where you must
|
||||||
|
|
||||||
|
Rust allows you to handle polymorphic code in two ways:
|
||||||
|
* **Generics / Static Dispatch**: compile-time, monomorphized per use.
|
||||||
|
* **Trait Objects / Dynamic Dispatch**: runtime vtable, single implementation.
|
||||||
|
|
||||||
|
Understanding the trade-offs lets you write faster, smaller and more flexible code.
|
||||||
|
|
||||||
|
## 6.1 [Generics](https://doc.rust-lang.org/book/ch10-00-generics.html)
|
||||||
|
|
||||||
|
Every programming language has tools for effectively handling the duplication of concepts. In Rust, one such tool is generics: abstract stand-ins for concrete types or other properties. We can express the behavior of generics or how they relate to other generics without knowing what will be in their place when compiling and running the code.
|
||||||
|
|
||||||
|
We use generics to create definitions for items like function signatures or structs, which we can then use with many different concrete data types. Let’s first look at how to define functions, structs, enums, and methods using generics. Generics can also be used to implement Type State Pattern and constrain a struct functionality to certain expected types, more on type state on [chapter 7](chapter_07.md).
|
||||||
|
|
||||||
|
[Generics by Examples](https://doc.rust-lang.org/rust-by-example/generics.html).
|
||||||
|
|
||||||
|
### Generics Performance
|
||||||
|
|
||||||
|
You might be wondering whether there is a runtime cost when using generic type parameters. The good news is that using generic types won’t make your program run any slower than it would with concrete types. Rust accomplishes this by performing monomorphization of the code using generics at compile time. Monomorphization is the process of turning generic code into specific code by filling in the concrete types that are used when compiled. The compiler checks for all occurrences of the generic parameter and generates code for the concrete types the generic code is called with.
|
||||||
|
|
||||||
|
## 6.2 Static Dispatch: `impl Trait` or `<T: Trait>`
|
||||||
|
|
||||||
|
A static dispatch is basically a constrained version of generics, a trait bounded generic, at compile-time it is able to check if your generic satisfies the declared traits.
|
||||||
|
|
||||||
|
### ✅ Best when:
|
||||||
|
* You want **zero runtime cost**, by paying the compile time cost.
|
||||||
|
* You need **tight loops or performance**.
|
||||||
|
* Your types are **known at compile time**.
|
||||||
|
* You are working with **single-use implementations** (monomorphized).
|
||||||
|
|
||||||
|
### 🏎️ Example: High-performance function with generic
|
||||||
|
```rust
|
||||||
|
fn specialized_sum<U: Sum + RandomMapping, T: Iterator<Item = U>>(iter: T) -> U {
|
||||||
|
iter.map(|x| x.random_mapping()).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
// or, equivalent, more modern
|
||||||
|
fn specialized_sum<U: Sum + RandomMapping>(iter: impl Iterator<Item = U>) -> U {
|
||||||
|
iter.map(|x| x.random_mapping()).sum()
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This is compiled into **specialized machine code** for each usage, fast and inlined.
|
||||||
|
|
||||||
|
## 6.3 Dynamic Dispatch: `dyn Trait`
|
||||||
|
|
||||||
|
Usually dynamic dispatch is used with some kind of pointer or a reference, like `Box<dyn Trait>`, `Arc<dyn Trait>` or `&dyn trait`.
|
||||||
|
|
||||||
|
### ✅ Best when:
|
||||||
|
* You absolutely need runtime polymorphism.
|
||||||
|
* You need to **store different implementations** in one collection.
|
||||||
|
* You want to **abstract internals behind a stable interface**.
|
||||||
|
* You are writing a **plugin-style architecture**.
|
||||||
|
|
||||||
|
> ❗ Closer to what you would get in an object oriented language and can have some heavy costs associated to it. Can avoid generics entirely and let you mix types that implement the same traits.
|
||||||
|
|
||||||
|
### 🚚 Example: Heterogeneous collection
|
||||||
|
|
||||||
|
```rust
|
||||||
|
trait Animal {
|
||||||
|
fn greet(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Dog;
|
||||||
|
impl Animal for Dog {
|
||||||
|
fn greet(&self) -> String {
|
||||||
|
"woof".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Cat;
|
||||||
|
impl Animal for Cat {
|
||||||
|
fn greet(&self) -> String {
|
||||||
|
"meow".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn all_animals_greeting(animals: Vec<Box<dyn Animal>>) {
|
||||||
|
for animal in animals {
|
||||||
|
println!("{}", animal.greet())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 6.4 Trade-off summary
|
||||||
|
|
||||||
|
| | Static Dispatch (impl Trait) | Dynamic Dispatch (dyn Trait) |
|
||||||
|
|------------------- |------------------------------ |---------------------------------- |
|
||||||
|
| Performance | ✅ Faster, inlined | ❌ Slower: vtable indirection |
|
||||||
|
| Compile time | ❌ Slower: monomorphization | ✅ Faster: shared code |
|
||||||
|
| Binary size | ❌ Larger: per-type codegen | ✅ Smaller |
|
||||||
|
| Flexibility | ❌ Rigid, one type at a time | ✅ Can mix types in collections |
|
||||||
|
| Use in trait fn() | ❌ Traits must be object-safe | ✅ Works with trait objects |
|
||||||
|
| Errors | ✅ Clearer | ❌ Erased types can confuse errors |
|
||||||
|
|
||||||
|
* Prefer generics/static dispatch when you control the call site and want performance.
|
||||||
|
* Use dynamic dispatch when you need abstraction, plugins or mixed types. 🚨 Runtime cost.
|
||||||
|
* If you are not sure, start with generics, trait bound them - then use `Box<dyn Trait>` when flexibility outweighs speed.
|
||||||
|
|
||||||
|
> Favor static dispatch until your trait needs to live behind a pointer.
|
||||||
|
|
||||||
|
## 6.5 Best Practices for Dynamic Dispatch
|
||||||
|
|
||||||
|
Dynamic dispatch `Ptr<dyn Trait>` is a powerful tool, but it also has significant performance trade-offs. You should only reach for it when **type erasure or runtime polymorphism** are essential. It is important to know when you need Trait Objects:
|
||||||
|
|
||||||
|
### ✅ Use Dynamic Dispatch When:
|
||||||
|
|
||||||
|
* You need heterogeneous types in a collection:
|
||||||
|
```rust
|
||||||
|
fn all_animals_greeting(animals: Vec<Box<dyn Animal>>) {
|
||||||
|
for animal in animals {
|
||||||
|
println!("{}", animal.greet())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* You want runtime plugins or hot-swappable components.
|
||||||
|
* You want to abstract internals from the caller (library design).
|
||||||
|
|
||||||
|
|
||||||
|
### ❌ Avoid Dynamic Dispatch When:
|
||||||
|
|
||||||
|
* You control the concrete types.
|
||||||
|
* You are writing code in performance critical paths.
|
||||||
|
* You can express the same logic in other ways while keeping simplicity, e.g. generics.
|
||||||
|
|
||||||
|
## 6.6 🚨 Trait Objects Ergonomics
|
||||||
|
|
||||||
|
* Prefer `&dyn Trait` over `Box<dyn Trait>` when you don't need ownership.
|
||||||
|
* Use `Arc<dyn Trait + Send + Sync>` for shared access across threads.
|
||||||
|
* Don't use `dyn Trait` if the trait has methods that return `Self`.
|
||||||
|
* **Avoid boxing too early**. Don't box inside structs unless you are sure it'll be beneficial or is required (recursive).
|
||||||
|
```rust
|
||||||
|
// ✅ Use generics when possible
|
||||||
|
struct Renderer<B: RenderBackend> {
|
||||||
|
backend: B
|
||||||
|
}
|
||||||
|
|
||||||
|
// ❌ Premature Boxing
|
||||||
|
struct Renderer {
|
||||||
|
backend: Box<dyn RenderBackend> // Boxing too early
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* If you must expose a `dyn trait` in a public API, `Box` at the boundary, not internally.
|
||||||
|
* **Object Safety**: You can only create `dyn Traits` from object-safe traits:
|
||||||
|
* It has **no generic methods**.
|
||||||
|
* It doesn't require `Self: Sized`.
|
||||||
|
* All method signatures use `&self`, `&mut self` or `self`.
|
||||||
|
```rust
|
||||||
|
// ✅ Object Safe
|
||||||
|
trait Runnable {
|
||||||
|
fn run(&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ❌ Not Object Safe
|
||||||
|
trait Factory {
|
||||||
|
fn create<T>() -> T; // generic methods are not allowed
|
||||||
|
}
|
||||||
|
```
|
||||||
222
skills/rust-best-practices/references/chapter_07.md
Normal file
222
skills/rust-best-practices/references/chapter_07.md
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
# Chapter 7 - Type State Pattern
|
||||||
|
|
||||||
|
Models state at compile time, preventing bugs by making illegal states unrepresentable. It takes advantage of the Rust generics and type system to create sub-types that can only be reached if a certain condition is achieved, making some operations illegal at compile time.
|
||||||
|
|
||||||
|
> Recently it became the standard design pattern of Rust programming. However, it is not exclusive to Rust, as it is achievable and has inspired other languages to do the same [swift](https://swiftology.io/articles/typestate/) and [typescript](https://catchts.com/type-state).
|
||||||
|
|
||||||
|
## 7.1 What is Type State Pattern?
|
||||||
|
|
||||||
|
**Type State Pattern** is a design pattern where you encode different **states** of the system as **types**, not as runtime flags or enums. This allows the compiler to enforce state transitions and prevent illegal actions at compile time. It also improves the developer experience, as developers only have access to certain functions based on the state of the type.
|
||||||
|
|
||||||
|
> Invalid states become compile errors instead of runtime bugs.
|
||||||
|
|
||||||
|
## 7.2 Why use it?
|
||||||
|
|
||||||
|
* Avoids runtime checks for state validity. If you reach certain states, you can make certain assumptions of the data you have.
|
||||||
|
* Models state transitions as type transitions. This is similar to a state machine, but in compile time.
|
||||||
|
* Prevents data misuse, e.g. using uninitialized objects.
|
||||||
|
* Improves API safety and correctness.
|
||||||
|
* The phantom data field is removed after compilation so no extra memory is allocated.
|
||||||
|
|
||||||
|
## 7.3 Simple Example: File State
|
||||||
|
|
||||||
|
[Github Example](https://github.com/apollographql/rust-best-practices/tree/main/examples/simple-type-state)
|
||||||
|
```rust
|
||||||
|
use std::{io, path::{Path, PathBuf}};
|
||||||
|
|
||||||
|
struct FileNotOpened;
|
||||||
|
struct FileOpened;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct File<State> {
|
||||||
|
/// Path to the opened file
|
||||||
|
path: PathBuf,
|
||||||
|
/// Open `File` handler
|
||||||
|
handle: Option<std::fs::File>,
|
||||||
|
/// Type state manager
|
||||||
|
_state: std::marker::PhantomData<State>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl File<FileNotOpened> {
|
||||||
|
/// `open` is the only entry point for this struct.
|
||||||
|
/// * When called with a valid path, it will return a `File<FileOpened>` with a valid `handler` and `path`
|
||||||
|
/// * `open` serves as an alternative to `new` and `defaults` methods (usable when your struct needs valid data to exist).
|
||||||
|
fn open(path: &Path) -> io::Result<File<FileOpened>> {
|
||||||
|
// If file is invalid, it will return `std::io::Error`
|
||||||
|
let file = std::fs::File::open(path)?;
|
||||||
|
Ok(
|
||||||
|
File {
|
||||||
|
path: path.to_path_buf(),
|
||||||
|
// Always valid
|
||||||
|
handle: Some(file),
|
||||||
|
_state: std::marker::PhantomData::<FileOpened>
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl File<FileOpened> {
|
||||||
|
/// Reads the content of the `File` as a `String`.
|
||||||
|
/// `read` can only be called by state `File<FileOpened>`
|
||||||
|
fn read(&mut self) -> io::Result<String> {
|
||||||
|
use io::Read;
|
||||||
|
|
||||||
|
let mut content = String::new();
|
||||||
|
let Some(handle)= self.handle.as_mut() else {
|
||||||
|
unreachable!("Safe to unwrap as state can only be reached when file is open");
|
||||||
|
};
|
||||||
|
handle.read_to_string(&mut content)?;
|
||||||
|
Ok(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the valid path buffer.
|
||||||
|
fn path(&self) -> &PathBuf {
|
||||||
|
&self.path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 7.4 Real-World Examples
|
||||||
|
|
||||||
|
### Builder Pattern with Compile-Time Guarantees
|
||||||
|
|
||||||
|
> Forces the user to **set required fields** before calling `.build()`.
|
||||||
|
|
||||||
|
[Github Example](https://github.com/apollographql/rust-best-practices/tree/main/examples/type-state-builder)
|
||||||
|
|
||||||
|
A type-state pattern can have more than one associated states:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
struct Unset;
|
||||||
|
struct Set;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Person {
|
||||||
|
name: String,
|
||||||
|
age: u8,
|
||||||
|
email: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Builder<NameState = Unset, AgeState = Unset> {
|
||||||
|
name: Option<String>,
|
||||||
|
age: u8,
|
||||||
|
email: Option<String>,
|
||||||
|
_maker_state: PhantomData<(NameState, AgeState)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Builder<Unset, Unset> {
|
||||||
|
const fn new() -> Self {
|
||||||
|
Self { name: None, age: 0, email: None, _maker_state: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<NameState> Builder<NameState, Unset> {
|
||||||
|
fn age(self, age: u8) -> Builder<NameState, Set> {
|
||||||
|
Builder { age, name: self.name, email: self.email, _maker_state: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<AgeState> Builder<Unset, AgeState> {
|
||||||
|
fn name(self, name: String) -> Builder<Set, AgeState> {
|
||||||
|
Builder { name: Some(name), age: self.age, email: self.email, _maker_state: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<NameState, AgeState> Builder<NameState, AgeState> {
|
||||||
|
fn email(self, email: String) -> Self {
|
||||||
|
Self { name: self.name, age: self.age, email: Some(email), _maker_state: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Builder<Set, Set> {
|
||||||
|
fn build(self) -> Person {
|
||||||
|
Person {
|
||||||
|
name: self.name.unwrap_or_else(|| unreachable!("Name is guaranteed to be set")),
|
||||||
|
age: self.age,
|
||||||
|
email: self.email,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Although a bit more verbose than a usual builder, this guarantees that all necessary fields are present (note that e-mail is optional field only present in the final builder).
|
||||||
|
|
||||||
|
#### Usage:
|
||||||
|
```rust
|
||||||
|
// ✅ Valid cases
|
||||||
|
let person: Person = Builder::new().name("name".to_string()).age(30).build();
|
||||||
|
let person: Person = Builder::new().age(30).name("name".to_string()).build();
|
||||||
|
let person: Person = Builder::new().age(30).name("name".to_string()).email("myself@email.com".to_string()).build();
|
||||||
|
|
||||||
|
// ❌ Invalid cases
|
||||||
|
let person: Person = Builder::new().name("name".to_string()).build(); // ❌ Compile error: Age required to `build`
|
||||||
|
let person: Person = Builder::new().age(30).build(); // ❌ Compile error: Name required to `build`
|
||||||
|
let person: Person = Builder::new().age(30).email("myself@email.com".to_string()).build(); // ❌ Compile error: Name required to `build`
|
||||||
|
let person: Person = Builder::new().age(10).age(15); // ❌ Compile error: Age was already set
|
||||||
|
let person: Person = Builder::new().build();// ❌ Compile error: Name and Age required to `build`
|
||||||
|
```
|
||||||
|
|
||||||
|
### Crates implementing Builder pattern
|
||||||
|
|
||||||
|
Some libraries are already implementing the builder pattern, like [bon-rs](https://docs.rs/bon/latest/bon/)
|
||||||
|
|
||||||
|
### Network Protocol State Machine
|
||||||
|
|
||||||
|
Illegal transitions like sending a message before connecting **simply don't compile**:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// Mock example
|
||||||
|
struct Disconnected;
|
||||||
|
struct Connected;
|
||||||
|
|
||||||
|
struct Client<State> {
|
||||||
|
stream: Option<std::net::TcpStream>,
|
||||||
|
_state: std::marker::PhantomData<State>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client<Disconnected> {
|
||||||
|
fn connect(addr: &str) -> std::io::Result<Client<Connected>> {
|
||||||
|
let stream = std::net::TcpStream::connect(addr)?;
|
||||||
|
Ok(Client {
|
||||||
|
stream: Some(stream),
|
||||||
|
_state: std::marker::PhantomData::<Connected>
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client<Connected> {
|
||||||
|
fn send(&mut self, msg: &str) {
|
||||||
|
use std::io::Write;
|
||||||
|
let Some(stream) = self.stream.as_mut() else {
|
||||||
|
unreachable!("Stream is guaranteed to be set");
|
||||||
|
};
|
||||||
|
stream.write_all(msg.as_bytes())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## 7.5 Pros and Cons
|
||||||
|
|
||||||
|
### ✅ Use Type-State Pattern When:
|
||||||
|
* You want **compile-time state safety**.
|
||||||
|
* You need to enforce **API constraints**.
|
||||||
|
* You are writing a library/crate that is heavily dependent on variants.
|
||||||
|
* You want to replace runtime booleans or enums with **type-safe code paths**.
|
||||||
|
* You need compile time correctness.
|
||||||
|
|
||||||
|
### ❌ Avoid it when:
|
||||||
|
* Writing trivial states like enums.
|
||||||
|
* Don't need type-safety.
|
||||||
|
* When it leads to overcomplicated generics.
|
||||||
|
* When runtime flexibility is required.
|
||||||
|
|
||||||
|
### 🚨 Downsides and Cautions
|
||||||
|
* Can lead to more **verbose solutions**.
|
||||||
|
* Can lead to **complex type signatures**.
|
||||||
|
* May require **unsafe** to return **variant outputs** based on different states.
|
||||||
|
* May require a bunch of duplication (e.g. same struct field reused).
|
||||||
|
* PhantomData is not intuitive for beginners and can feel a bit hacky.
|
||||||
|
|
||||||
|
> Use this pattern when it **saves bugs, increases safety or simplifies logic**, not just for cleverness.
|
||||||
252
skills/rust-best-practices/references/chapter_08.md
Normal file
252
skills/rust-best-practices/references/chapter_08.md
Normal file
@@ -0,0 +1,252 @@
|
|||||||
|
# Chapter 8 - Comments vs Documentation
|
||||||
|
|
||||||
|
> Clear code beats clear comments. However, when the why isn't obvious, comment it plainly - or link to where you can read more context.
|
||||||
|
|
||||||
|
## 8.1 Comments vs Documentation: Know the Difference
|
||||||
|
|
||||||
|
| Purpose | Use `// comment` | Use `/// doc` or `//! crate doc` |
|
||||||
|
|-------------- |------------------------------------------- |---------------------------------------------------------------- |
|
||||||
|
| Describe Why | ✅ Yes - explains tricky reasoning | ❌ Not for documentation |
|
||||||
|
| Describe API | ❌ Not useful | ✅ Yes - public interfaces, usage, details, errors, panics |
|
||||||
|
| Maintainable | 🚨 Often becomes obsolete and hard to reason | ✅ Tied to code, appears in generated docs and can run test cases |
|
||||||
|
| Visibility | Local development only | Exported to users and tools like `cargo doc` |
|
||||||
|
|
||||||
|
## 8.2 When to use comments
|
||||||
|
|
||||||
|
Use `//` comments (double slashed) when something can't be expressed clearly in code, like:
|
||||||
|
* **Safety Guarantees**, some of which can be better expressed with code conditionals.
|
||||||
|
* Workarounds or **Optimizations**.
|
||||||
|
* Legacy or **platform-specific** behaviors. Some of them can be expressed with `#[cfg(..)]`.
|
||||||
|
* Links to **Design Docs** or **ADRs**.
|
||||||
|
* Assumptions or **gotchas** that aren't obvious.
|
||||||
|
|
||||||
|
> Name your comments! For example, a comment regarding a safety guarantee should start with `// SAFETY: ...`.
|
||||||
|
|
||||||
|
### ✅ Good comment:
|
||||||
|
```rust
|
||||||
|
// SAFETY: `ptr` is guaranteed to be non-null and aligned by caller
|
||||||
|
unsafe { std::ptr::copy_nonoverlapping(src, dst, len); }
|
||||||
|
```
|
||||||
|
|
||||||
|
### ✅ Design context comment:
|
||||||
|
```rust
|
||||||
|
// CONTEXT: Reuse root cert store across subgraphs to avoid duplicate OS calls:
|
||||||
|
// [ADR-12](link/to/adr-12): TLS Performance on MacOS
|
||||||
|
```
|
||||||
|
|
||||||
|
## 8.3 When comments get in the way
|
||||||
|
|
||||||
|
Avoid comments that:
|
||||||
|
* Restate obvious things (`// increment i by 1 for the next loop`).
|
||||||
|
* Can grow stale over time.
|
||||||
|
* `TODO`s without actions (links to some versioned issue).
|
||||||
|
* Could be replaced by better naming or smaller functions.
|
||||||
|
|
||||||
|
### ❌ Bad comment:
|
||||||
|
```rust
|
||||||
|
fn compute(counter: &mut usize) {
|
||||||
|
// increment by 1
|
||||||
|
*counter += 1;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### ❌ Too long or outdated
|
||||||
|
```rust
|
||||||
|
// Originally written in 2028 for some now-defunct platform
|
||||||
|
```
|
||||||
|
|
||||||
|
## 8.4 Don't Write Living Documentation (living comments)
|
||||||
|
|
||||||
|
Comments as a "living documentation" is a **dangerous myth**, as comments are **not free**:
|
||||||
|
* They **rot** - nobody compiles comments.
|
||||||
|
* They **mislead** - readers usually assume they are true with no critique, e.g. "the other developer knows this code better than I do".
|
||||||
|
* They **go stale** - unless maintained with the code, they become irrelevant.
|
||||||
|
* They are **noisy** - comments can clutter your code with multiple unnecessary lines.
|
||||||
|
|
||||||
|
If something deserves to live beyond a PR, put it in:
|
||||||
|
* An **ADR** (Architectural Design Record).
|
||||||
|
* A Design Document.
|
||||||
|
* Document it **in code** by using types, doc comments, examples, renaming code blocks into cleaner functions.
|
||||||
|
* Add tests to cover and explain the change.
|
||||||
|
|
||||||
|
> ### 🚨 If you find a comment, **read it in context**. Does it still make sense? If not, remove or update it, or ask for help. Comments should bother you.
|
||||||
|
|
||||||
|
## 8.5 Replace Comments with Code
|
||||||
|
|
||||||
|
Instead of long commented blocks, break logic into named helper functions:
|
||||||
|
|
||||||
|
#### ❌ Commented code block:
|
||||||
|
```rust
|
||||||
|
fn save_user(&self) -> Result<(), MyError> {
|
||||||
|
// check if the user is authenticated
|
||||||
|
if self.is_authenticated() {
|
||||||
|
// serialize user data
|
||||||
|
let data = serde_json::to_string(self)?;
|
||||||
|
// write to file
|
||||||
|
std::fs::write(self.path(), data)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
**✅ Extract for clarity**:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn save_auth_user(&self) -> Result<PathBuf, MyError> {
|
||||||
|
if self.is_authenticated() {
|
||||||
|
let path = self.path();
|
||||||
|
let serialized_user = serde_json::to_string(self)?;
|
||||||
|
std::fs::write(path, serialized_user)?;
|
||||||
|
Ok(path)
|
||||||
|
} else {
|
||||||
|
Err(MyError::UserNotAuthenticated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
> ❗ Extract when the **name adds meaning** — not to deduplicate a couple of similar-looking lines. See [Chapter 1, §1.8](chapter_01.md#18-when-to-extract-a-function-and-when-not-to) for when duplication is the better trade-off.
|
||||||
|
|
||||||
|
## 8.6 `TODO` should become issues
|
||||||
|
|
||||||
|
Don't leave `// TODO:` scattered around the codebase with no owner. Instead:
|
||||||
|
1. File Github Issue or Jira Ticket. (Prefer github issues on public repositories).
|
||||||
|
2. Reference the issue in the code:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
// TODO(issue #42): Remove workaround after bugfix
|
||||||
|
```
|
||||||
|
|
||||||
|
This makes `TODO`s trackable, actionable and visible to everyone.
|
||||||
|
|
||||||
|
## 8.7 When to use doc comments
|
||||||
|
|
||||||
|
Use `///` doc comments to document:
|
||||||
|
* All **public functions, structs, traits, enums**.
|
||||||
|
* Their purpose, their usage and their behaviors.
|
||||||
|
* Anything developers need to understand how to use it correctly.
|
||||||
|
* Add context that related to `Errors` and `Panics`.
|
||||||
|
* Plenty of examples.
|
||||||
|
|
||||||
|
### ✅ Good doc comment:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
/// Loads [`User`] profile from disk
|
||||||
|
///
|
||||||
|
/// # Error
|
||||||
|
/// - Returns [`MyError`] if the file is missing [`MyError::FileNotFound`].
|
||||||
|
/// - Returns [`MyError`] if the content is an invalid Json, [`MyError::InvalidJson`].
|
||||||
|
fn load_user(path: &Path) -> Result<User, MyError> {...}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Doc comments can also include examples, links and even tests:**
|
||||||
|
|
||||||
|
```rust
|
||||||
|
/// Returns the square of the integer part of any number.
|
||||||
|
/// Square is limited to `u128`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// assert_eq!(square(4.3), 16)
|
||||||
|
/// ```
|
||||||
|
fn square(x: impl ToInt) -> u128 { ... }
|
||||||
|
```
|
||||||
|
|
||||||
|
## 8.8 Documentation in Rust: How, When and Why
|
||||||
|
|
||||||
|
Rust provides **first-class documentation tooling** via rustdoc, which makes documenting your code a key part of writing idiomatic and maintainable rust. There are doc specific lints to help with documentation, like:
|
||||||
|
|
||||||
|
| Lint | Description |
|
||||||
|
|-------------- |------------------------------------------- |
|
||||||
|
| [missing_docs](https://doc.rust-lang.org/rustdoc/lints.html#missing_docs) | Warns that a public functions, struct, const, enum has missing documentation |
|
||||||
|
| [broken_intra_doc_links](https://doc.rust-lang.org/rustdoc/lints.html#broken_intra_doc_links) | Detects if an internal documentation link is broken. Especially useful when things are renamed. |
|
||||||
|
| [empty_docs](https://rust-lang.github.io/rust-clippy/master/#empty_docs) | Disallow empty docs - preventing bypass of `missing_docs` |
|
||||||
|
| [missing_panics_doc](https://rust-lang.github.io/rust-clippy/master/#missing_panics_doc) | Warns that documentation should have a `# Panics` section if function can panic |
|
||||||
|
| [missing_errors_doc](https://rust-lang.github.io/rust-clippy/master/#missing_errors_doc) | Warns that documentation should have a `# Errors` section if function returns a `Result` explaining `Err` conditions |
|
||||||
|
| [missing_safety_doc](https://rust-lang.github.io/rust-clippy/master/#missing_safety_doc) | Warns that documentation should have a `# Safety` section if public facing functions have visible unsafe blocks |
|
||||||
|
|
||||||
|
|
||||||
|
### Difference between `///` and `//!`
|
||||||
|
|
||||||
|
| Style | Used for | Scope |Example |
|
||||||
|
|---------- |------------------------------ |------------------------------------------- |---------------------------------------------------------------- |
|
||||||
|
| `///` | Line doc comment | Public items like struct, fn, enum, consts | Documenting, giving context and usage to `fn`, `struct`, `enum`, etc |
|
||||||
|
| `//!` | Module level doc comment | Modules or entire crates | Explaining crate/module purpose with common use cases and quickstart |
|
||||||
|
|
||||||
|
### `///` Item level documentation
|
||||||
|
|
||||||
|
Use `///` for functions, structs, traits, enums, const, etc:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
/// Adds two numbers together.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let result = my_crate::add(2, 3);
|
||||||
|
/// assert_eq!(result, 5);
|
||||||
|
/// ```
|
||||||
|
pub fn add(a: i32, b: i32) -> i32 {
|
||||||
|
a + b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* ✅ Write clear and descriptive **What it does** and **how to use it**.
|
||||||
|
* ✅ Use `# Examples` section to better explain **how to use it**.
|
||||||
|
* ✅ Prefer writing examples that can be tested via `cargo test`, even if you have to hide their output with starting `#`:
|
||||||
|
```rust
|
||||||
|
/// ```
|
||||||
|
/// let result = my_crate::add(2, 3);
|
||||||
|
/// # assert_eq!(result, 5);
|
||||||
|
/// ```
|
||||||
|
```
|
||||||
|
* ✅ Use `# Panics`, `# Errors` and `# Safety` sections when relevant.
|
||||||
|
* Add relevant context to the type.
|
||||||
|
|
||||||
|
### `//!` Module/Crate level Documentation
|
||||||
|
|
||||||
|
Use `//!` when you want to document the **purpose of a module or a crate**. It is placed at the top of a `lib.rs` or `mod.rs` file, for example `engine/mod.rs`:
|
||||||
|
```rust
|
||||||
|
//! This module implements a custom chess engine.
|
||||||
|
//!
|
||||||
|
//! It handles board state, move generation and check detection.
|
||||||
|
//!
|
||||||
|
//! # Example
|
||||||
|
//! ```
|
||||||
|
//! let board = chess::engine::Board::default();
|
||||||
|
//! assert!(board.is_valid());
|
||||||
|
//! ```
|
||||||
|
```
|
||||||
|
|
||||||
|
## 8.9 Checklist for Documentation coverage
|
||||||
|
|
||||||
|
📦 Crate-Level (lib.rs)
|
||||||
|
- [ ] `//!` doc at top explains **what the crate does**, and **what problems it solves**.
|
||||||
|
- [ ] Includes crate-level `# Examples` or pointers to modules.
|
||||||
|
📁 Modules (mod.rs or inline)
|
||||||
|
- [ ] `//!` doc explains **what this module is for**, its **exports**, and **invariants**.
|
||||||
|
- [ ] Avoid repeating doc comments on re-exported items unless clarification is needed.
|
||||||
|
🧱 Structs, Enums, Traits
|
||||||
|
- `///` doc explains:
|
||||||
|
- [ ] The role this type plays.
|
||||||
|
- [ ] Invariants or expectations.
|
||||||
|
- [ ] Example construction or usage.
|
||||||
|
- [ ] Consider using [`#[non_exhaustive]`](https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute) if external users may match on it.
|
||||||
|
🔧 Functions and Methods
|
||||||
|
- `///` doc covers:
|
||||||
|
- [ ] What it does.
|
||||||
|
- [ ] Parameters and their meaning.
|
||||||
|
- [ ] Return value behavior.
|
||||||
|
- [ ] Edge cases (`# Panics`, `# Errors`).
|
||||||
|
- [ ] Usage example, `# Examples`.
|
||||||
|
📑 Traits
|
||||||
|
- [ ] Explain the **purpose** of the trait (marker? dynamic dispatch?).
|
||||||
|
- [ ] Doc for each method — include **when/why** to implement it.
|
||||||
|
- [ ] Document clearly default implemented methods and when to override.
|
||||||
|
📦 Public Constants
|
||||||
|
- [ ] Document what they configure and when you'd want to use them.
|
||||||
|
|
||||||
|
### 📌 Best Practices
|
||||||
|
* ✅ Use examples generously — they double as test cases.
|
||||||
|
* ✅ Prefer clarity over formality — it’s for humans, not machines.
|
||||||
|
* ✅ Prefer doc comments to explain usage, and leave implementation details to code comments if needed.
|
||||||
|
* ✅ Use `cargo doc --open` to check your output often.
|
||||||
|
* ✅ Add `#![deny(missing_docs)]` and other relevant doc lints in top-level modules if you want to enforce full doc coverage.
|
||||||
256
skills/rust-best-practices/references/chapter_09.md
Normal file
256
skills/rust-best-practices/references/chapter_09.md
Normal file
@@ -0,0 +1,256 @@
|
|||||||
|
# Chapter 9 - Understanding Pointers
|
||||||
|
|
||||||
|
Many higher level languages hide memory management, typically **passing by value** (copy data) or **passing by reference** (reference to shared data) without worrying about allocation, heap, stack, ownership and lifetimes, it is all delegated to the garbage collector or VM. Here is a comparison on this topic between a few languages:
|
||||||
|
|
||||||
|
### 📌 Language Comparison
|
||||||
|
|
||||||
|
| Language | Value Types | Reference/Pointer Types | Async Model & Types | Manual Memory |
|
||||||
|
|------------ |------------------------------------- |----------------------------------------------------------- |---------------------------------------------------------------------------- |------------------------------ |
|
||||||
|
| Python | None | Everything is a reference | async def, await, Task, coroutines and asyncio.Future | ❌ Not Allowed |
|
||||||
|
| Javascript | Primitives | Objects | `async/await`, `Promise`, `setTimeout`. single threaded event loop | ❌ Not Allowed |
|
||||||
|
| Java | Primitives | Objects | `Future<T>`, threads, Loom (green threads) | ❌ Almost none & not recommended |
|
||||||
|
| Go | Values are copied unless using `&T` | Pointers (`*T`, `&T`), escape analysis | goroutines, `channels`, `sync.Mutex`, `context.Context` | ⚠️ Limited |
|
||||||
|
| C | Primitives and structs supported | Raw pointers `T*` and `*void` | Threads, event loops (`libuv`, `libevent`) | ✅ Fully |
|
||||||
|
| C++ | Primitives and references | Raw `T*` and smart pointers `shared_ptr` and `unique_ptr` | threads, `std::future`, `std::async`, (since c++ 20 `co_await/coroutines`) | ✅ Mostly |
|
||||||
|
| Rust | Primitives, Arrays, `impl Copy` | `&T`, `&mut T`, `Box<T>`, `Arc<T>` | `async/await`, `tokio`, `Future`, `JoinHandle`, `Send + Sync` | ✅🔒 Safe and Explicit |
|
||||||
|
|
||||||
|
## 9.1 Thread Safety
|
||||||
|
|
||||||
|
Rust tracks pointers using `Send` and `Sync` traits:
|
||||||
|
- `Send` means data can move across threads.
|
||||||
|
- `Sync` means data can be referenced from multiple threads.
|
||||||
|
|
||||||
|
> A pointer is thread-safe only if the data behind it is.
|
||||||
|
|
||||||
|
| Pointer Type | Short Description | Send + Sync? | Main Use |
|
||||||
|
|---------------- |--------------------------------------------------------------------------- |-------------------------------------- |------------ |
|
||||||
|
| `&T` | Shared reference | Yes | Shared access |
|
||||||
|
| `&mut T` | Exclusive mutable reference | No, not Send | Exclusive mutation |
|
||||||
|
| `Box<T>` | Heap-allocated owning pointer | Yes, if T: Send + Sync | Heap allocation |
|
||||||
|
| `Rc<T>` | Single-threaded ref counted pointer | No, neither | Multiple owners (single-thread) |
|
||||||
|
| `Arc<T>` | Atomic ref counter pointer | Yes | Multiple owners (multi-thread) |
|
||||||
|
| `Cell<T>` | Interior mutability for copy types | No, not Sync | Shared mutable, non-threaded |
|
||||||
|
| `RefCell<T>` | Interior mutability (dynamic borrow checker) | No, not Sync | Shared mutable, non-threaded |
|
||||||
|
| `Mutex<T>` | Thread-safe interior mutability with exclusive access | Yes | Shared mutable, threaded |
|
||||||
|
| `RwLock<T>` | Thread-safe shared readonly access OR exclusive mutable access | Yes | Shared mutable, threaded |
|
||||||
|
| `OnceCell<T>` | Single-thread one-time initialization container (interior mutability ONCE) | No, not Sync | Simple lazy value initialization |
|
||||||
|
| `LazyCell<T>` | A lazy version of `OnceCell<T>` that calls function closure to initialize | No, not Sync | Complex lazy value initialization
|
||||||
|
| `OnceLock<T>` | Thread-safe version of `OnceCell<T>` | Yes | Multi-thread single init |
|
||||||
|
| `LazyLock<T>` | Thread-safe version of `LazyCell<T>` | Yes | Multi-thread complex init |
|
||||||
|
| `*const T/*mut T` | Raw Pointers | No, user must ensure safety manually | Raw memory / FFI |
|
||||||
|
|
||||||
|
## 9.2 When to use pointers:
|
||||||
|
|
||||||
|
### `&T` - Shared Borrow:
|
||||||
|
|
||||||
|
Probably the most common type in a Rust code base, it is **Safe, with no mutation** and allows **multiple readers**.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let data: String = String::from_str("this is a string").unwrap();
|
||||||
|
|
||||||
|
print_len(&data);
|
||||||
|
print_capacity(&data);
|
||||||
|
print_bytes(&data);
|
||||||
|
|
||||||
|
fn print_len(s: &str) {
|
||||||
|
println!("{}", s.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_capacity(s: &String) {
|
||||||
|
println!("{}", s.capacity())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn print_bytes(s: &String) {
|
||||||
|
println!("{:?}", s.as_bytes())
|
||||||
|
}
|
||||||
|
```
|
||||||
|
### `&mut T` - Exclusive Borrow:
|
||||||
|
|
||||||
|
Probably the most common *mutable* type in a Rust code base, it is **Safe, but only allows one mutable borrow at a time**.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let mut data: String = String::from_str("this is a string").unwrap();
|
||||||
|
mark_update(&mut data);
|
||||||
|
|
||||||
|
fn mark_update(s: &mut String) {
|
||||||
|
s.push_str("_update");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`Box<T>`](https://doc.rust-lang.org/std/boxed/struct.Box.html) - Heap Allocated
|
||||||
|
|
||||||
|
Single-owner heap-allocated data, great for recursive types and large structs.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub enum MySubBoxedEnum<T> {
|
||||||
|
Single(T),
|
||||||
|
Double(Box<MySubBoxedEnum<T>>, Box<MySubBoxedEnum<T>>),
|
||||||
|
Multi(Vec<T>), // Note that Vec is already a boxed value
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`Rc<T>`](https://doc.rust-lang.org/std/rc/struct.Rc.html) - Reference Counter (single-thread)
|
||||||
|
|
||||||
|
You need multiple references to data in a single thread. Most common example is linked-list implementation.
|
||||||
|
|
||||||
|
### [`Arc<T>`](https://doc.rust-lang.org/std/sync/struct.Arc.html) - Atomic Reference Counter (multi-thread)
|
||||||
|
|
||||||
|
You need multiple references to data in multiple threads. Most common use case is sharing readonly Vec across thread with `Arc<[T]>` and wrapping a `Mutex` so it can be easily shared across threads, `Arc<Mutex<T>>`.
|
||||||
|
|
||||||
|
### [`RefCell<T>`](https://doc.rust-lang.org/std/cell/struct.RefCell.html) - Runtime checked interior mutability
|
||||||
|
|
||||||
|
Used when you need shared access and the ability to mutate data, borrow rules are enforced at runtime. **It may panic!**.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::cell::RefCell;
|
||||||
|
let x = RefCell::new(42);
|
||||||
|
*x.borrow_mut() += 1;
|
||||||
|
|
||||||
|
assert_eq!(&*x.borrow(), 42, "Not meaning of life");
|
||||||
|
```
|
||||||
|
|
||||||
|
Panic example:
|
||||||
|
```rust
|
||||||
|
use std::cell::RefCell;
|
||||||
|
let x = RefCell::new(42);
|
||||||
|
|
||||||
|
let borrow = x.borrow();
|
||||||
|
|
||||||
|
let mutable = x.borrow_mut();
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`Cell<T>`](https://doc.rust-lang.org/std/cell/struct.Cell.html) - Copy-only interior mutability
|
||||||
|
|
||||||
|
Somewhat the fast and safe version of `RefCell`, but it is limited to types that implement the `Copy` trait:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::cell::Cell;
|
||||||
|
|
||||||
|
struct SomeStruct {
|
||||||
|
regular_field: u8,
|
||||||
|
special_field: Cell<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let my_struct = SomeStruct {
|
||||||
|
regular_field: 0,
|
||||||
|
special_field: Cell::new(1),
|
||||||
|
};
|
||||||
|
|
||||||
|
let new_value = 100;
|
||||||
|
|
||||||
|
// ERROR: `my_struct` is immutable
|
||||||
|
// my_struct.regular_field = new_value;
|
||||||
|
|
||||||
|
// WORKS: although `my_struct` is immutable, `special_field` is a `Cell`,
|
||||||
|
// which can always be mutated with copy values
|
||||||
|
my_struct.special_field.set(new_value);
|
||||||
|
assert_eq!(my_struct.special_field.get(), new_value);
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`Mutex<T>`](https://doc.rust-lang.org/std/sync/struct.Mutex.html) - Thread-safe mutability
|
||||||
|
|
||||||
|
An exclusive access pointer that allows a thread to read/write the data contained inside. It is usually wrapped in an `Arc` to allow shared access to the Mutex.
|
||||||
|
|
||||||
|
### [`RwLock<T>`](https://doc.rust-lang.org/std/sync/struct.RwLock.html) - Thread-safe mutability
|
||||||
|
|
||||||
|
Similar to a `Mutex`, but it allows multiple threads to read it OR a single thread to write. It is usually wrapped in an `Arc` to allow shared access to the RwLock.
|
||||||
|
|
||||||
|
|
||||||
|
### [`*const T/*mut T`](https://doc.rust-lang.org/std/primitive.pointer.html) - Raw pointers
|
||||||
|
|
||||||
|
Inherently **unsafe** and necessary for FFI. Rust makes their usage explicit to avoid accidental misuse and unwilling manual memory management.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
let x = 5;
|
||||||
|
let ptr = &x as *const i32
|
||||||
|
unsafe {
|
||||||
|
println!("PTR is {}", *ptr)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`OnceCell`](https://doc.rust-lang.org/std/cell/struct.OnceCell.html) - Single-thread single initialization container
|
||||||
|
|
||||||
|
Most useful when you need to share a configuration between multiple data structures.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::{cell::OnceCell, rc::Rc};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
struct MyStruct {
|
||||||
|
distance: usize,
|
||||||
|
root: Option<Rc<OnceCell<MyStruct>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let root = MyStruct::default();
|
||||||
|
let root_cell = Rc::new(OnceCell::new());
|
||||||
|
if let Err(previous) = root_cell.set(root) {
|
||||||
|
eprintln!("Previous Root {previous:?}");
|
||||||
|
}
|
||||||
|
let child_1 = MyStruct{
|
||||||
|
distance: 1,
|
||||||
|
root: Some(root_cell.clone())
|
||||||
|
};
|
||||||
|
|
||||||
|
let child_2 = MyStruct{
|
||||||
|
distance: 2,
|
||||||
|
root: Some(root_cell.clone())
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
println!("Child 1: {child_1:?}");
|
||||||
|
println!("Child 2: {child_2:?}");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`LazyCell`](https://doc.rust-lang.org/std/cell/struct.LazyCell.html) - Lazy initialization of `OnceCell`
|
||||||
|
|
||||||
|
Useful when the initialized data can be delayed to when it is actually being called.
|
||||||
|
|
||||||
|
### [`OnceLock`](https://doc.rust-lang.org/std/sync/struct.OnceLock.html) - thread-safe `OnceCell`
|
||||||
|
|
||||||
|
Useful when you need a `static` value.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
|
static CELL: OnceLock<usize> = OnceLock::new();
|
||||||
|
|
||||||
|
// `OnceLock` has not been written to yet.
|
||||||
|
assert!(CELL.get().is_none());
|
||||||
|
|
||||||
|
// Spawn a thread and write to `OnceLock`.
|
||||||
|
std::thread::spawn(|| {
|
||||||
|
let value = CELL.get_or_init(|| 12345);
|
||||||
|
assert_eq!(value, &12345);
|
||||||
|
})
|
||||||
|
.join()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// `OnceLock` now contains the value.
|
||||||
|
assert_eq!(
|
||||||
|
CELL.get(),
|
||||||
|
Some(&12345),
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### [`LazyLock`](https://doc.rust-lang.org/std/sync/struct.LazyLock.html) - thread-safe `LazyCell`
|
||||||
|
|
||||||
|
Similar to `OnceLock`, but the static value is a bit more complex to initialize.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
|
static CONFIG: LazyLock<HashMap<String, T>> = LazyLock::new(|| {
|
||||||
|
let data = read_config();
|
||||||
|
let mut config: HashMap<String, T> = data.into();
|
||||||
|
config.insert("special_case", T::Default());
|
||||||
|
config
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = &*CONFIG;
|
||||||
|
```
|
||||||
|
|
||||||
|
## References
|
||||||
|
- [Mara Bos - Rust Atomics and Locks](https://marabos.nl/atomics/)
|
||||||
|
- [Semicolon video on pointers](https://www.youtube.com/watch?v=Ag_6Q44PBNs)
|
||||||
12
skills/rust-best-practices/references/zz_final_notes.md
Normal file
12
skills/rust-best-practices/references/zz_final_notes.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# Final notes
|
||||||
|
|
||||||
|
* Rust doesn’t prevent mistakes; it makes it easier to catch them early.
|
||||||
|
* The compiler is not your enemy, it is a powerful tool. Think of it as your best friend.
|
||||||
|
* Keep learning, every new Rust version has interesting new features to look out for and every edition brings major improvements.
|
||||||
|
* Keep an eye on [Rust blog](https://blog.rust-lang.org/) for new versions
|
||||||
|
* [Rust Official API Guidelines](https://rust-lang.github.io/api-guidelines/about.html)
|
||||||
|
* [Rust Analyzer Style Guide](https://rust-analyzer.github.io/book/contributing/style.html)
|
||||||
|
|
||||||
|
## Similar projects
|
||||||
|
|
||||||
|
* [mre/idiomatic-rust](https://github.com/mre/idiomatic-rust/tree/master)
|
||||||
Reference in New Issue
Block a user