agy-superpowers 5.1.4 → 5.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/agent/rules/debug-confirmation-policy.md +34 -0
- package/template/agent/rules/language-matching.md +32 -0
- package/template/agent/skills/rust-developer/SKILL.md +281 -0
- package/template/agent/skills/rust-developer/references/rust-rules/_sections.md +231 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-clone-excessive.md +124 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-collect-intermediate.md +131 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-empty-catch.md +132 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-expect-lazy.md +95 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-format-hot-path.md +141 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-index-over-iter.md +125 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-lock-across-await.md +127 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-over-abstraction.md +120 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-panic-expected.md +131 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-premature-optimize.md +156 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-string-for-str.md +122 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-stringly-typed.md +167 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-type-erasure.md +134 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-unwrap-abuse.md +143 -0
- package/template/agent/skills/rust-developer/references/rust-rules/anti-vec-for-slice.md +121 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-builder-must-use.md +143 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-builder-pattern.md +187 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-common-traits.md +165 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-default-impl.md +177 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-extension-trait.md +163 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-from-not-into.md +146 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-impl-asref.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-impl-into.md +160 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-must-use.md +125 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-newtype-safety.md +162 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-non-exhaustive.md +177 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-parse-dont-validate.md +184 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-sealed-trait.md +168 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-serde-optional.md +182 -0
- package/template/agent/skills/rust-developer/references/rust-rules/api-typestate.md +199 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-bounded-channel.md +175 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-broadcast-pubsub.md +185 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-cancellation-token.md +203 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-clone-before-await.md +171 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-join-parallel.md +158 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-joinset-structured.md +195 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-mpsc-queue.md +171 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-no-lock-await.md +156 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-oneshot-response.md +191 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-select-racing.md +198 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-spawn-blocking.md +154 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-tokio-fs.md +167 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-tokio-runtime.md +169 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-try-join.md +172 -0
- package/template/agent/skills/rust-developer/references/rust-rules/async-watch-latest.md +189 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-all-public.md +113 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-cargo-metadata.md +147 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-errors-section.md +122 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-examples-section.md +161 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-hidden-setup.md +149 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-intra-links.md +138 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-link-types.md +169 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-module-inner.md +116 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-panics-section.md +128 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-question-mark.md +136 -0
- package/template/agent/skills/rust-developer/references/rust-rules/doc-safety-section.md +131 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-anyhow-app.md +179 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-context-chain.md +144 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-custom-type.md +152 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-doc-errors.md +145 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-expect-bugs-only.md +133 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-from-impl.md +152 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-lowercase-msg.md +124 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-no-unwrap-prod.md +115 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-question-mark.md +151 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-result-over-panic.md +130 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-source-chain.md +155 -0
- package/template/agent/skills/rust-developer/references/rust-rules/err-thiserror-lib.md +171 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-cargo-metadata.md +138 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-deny-correctness.md +107 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-missing-docs.md +154 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-pedantic-selective.md +118 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-rustfmt-check.md +157 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-unsafe-doc.md +133 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-complexity.md +131 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-perf.md +136 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-style.md +135 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-warn-suspicious.md +122 -0
- package/template/agent/skills/rust-developer/references/rust-rules/lint-workspace-lints.md +172 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-arena-allocator.md +168 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-arrayvec.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-assert-type-size.md +168 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-avoid-format.md +147 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-box-large-variant.md +158 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-boxed-slice.md +139 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-clone-from.md +147 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-compact-string.md +149 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-reuse-collections.md +174 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-smaller-integers.md +159 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-smallvec.md +138 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-thinvec.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-with-capacity.md +156 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-write-over-format.md +172 -0
- package/template/agent/skills/rust-developer/references/rust-rules/mem-zero-copy.md +164 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-acronym-word.md +99 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-as-free.md +104 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-consts-screaming.md +94 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-crate-no-rs.md +78 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-funcs-snake.md +76 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-into-ownership.md +123 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-is-has-bool.md +127 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-iter-convention.md +129 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-iter-method.md +131 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-iter-type-match.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-lifetime-short.md +86 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-no-get-prefix.md +154 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-to-expensive.md +118 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-type-param-single.md +92 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-types-camel.md +65 -0
- package/template/agent/skills/rust-developer/references/rust-rules/name-variants-camel.md +101 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-bounds-check.md +161 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-cache-friendly.md +187 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-codegen-units.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-cold-unlikely.md +152 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-inline-always-rare.md +141 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-inline-never-cold.md +181 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-inline-small.md +160 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-likely-hint.md +171 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-lto-release.md +130 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-pgo-profile.md +167 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-simd-portable.md +144 -0
- package/template/agent/skills/rust-developer/references/rust-rules/opt-target-cpu.md +154 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-arc-shared.md +141 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-borrow-over-clone.md +95 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-clone-explicit.md +135 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-copy-small.md +124 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-cow-conditional.md +135 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-lifetime-elision.md +134 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-move-large.md +134 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-mutex-interior.md +105 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-rc-single-thread.md +65 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-refcell-interior.md +97 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-rwlock-readers.md +122 -0
- package/template/agent/skills/rust-developer/references/rust-rules/own-slice-over-vec.md +119 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-black-box-bench.md +153 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-chain-avoid.md +136 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-collect-into.md +133 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-collect-once.md +120 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-drain-reuse.md +137 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-entry-api.md +134 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-extend-batch.md +150 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-iter-lazy.md +123 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-iter-over-index.md +113 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-profile-first.md +175 -0
- package/template/agent/skills/rust-developer/references/rust-rules/perf-release-profile.md +149 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-bin-dir.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-flat-small.md +133 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-lib-main-split.md +148 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-mod-by-feature.md +130 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-mod-rs-dir.md +120 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-prelude-module.md +155 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-pub-crate-internal.md +139 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-pub-super-parent.md +135 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-pub-use-reexport.md +162 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-workspace-deps.md +186 -0
- package/template/agent/skills/rust-developer/references/rust-rules/proj-workspace-large.md +162 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-arrange-act-assert.md +160 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-cfg-test-module.md +151 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-criterion-bench.md +171 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-descriptive-names.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-doctest-examples.md +168 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-fixture-raii.md +151 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-integration-dir.md +144 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-mock-traits.md +189 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-mockall-mocking.md +226 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-proptest-properties.md +161 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-should-panic.md +130 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-tokio-async.md +154 -0
- package/template/agent/skills/rust-developer/references/rust-rules/test-use-super.md +127 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-enum-states.md +154 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-generic-bounds.md +142 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-never-diverge.md +146 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-newtype-ids.md +160 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-newtype-validated.md +159 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-no-stringly.md +144 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-option-nullable.md +137 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-phantom-marker.md +188 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-repr-transparent.md +143 -0
- package/template/agent/skills/rust-developer/references/rust-rules/type-result-fallible.md +131 -0
- package/template/agent/skills/systematic-debugging/SKILL.md +17 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# doc-intra-links
|
|
2
|
+
|
|
3
|
+
> Use intra-doc links to reference types and items
|
|
4
|
+
|
|
5
|
+
## Why It Matters
|
|
6
|
+
|
|
7
|
+
Intra-doc links (`[TypeName]`, `[method](Self::method)`) create clickable references in generated documentation. They're verified at doc-build time, catching broken links early. Unlike URL links, they automatically update when items are renamed or moved.
|
|
8
|
+
|
|
9
|
+
## Bad
|
|
10
|
+
|
|
11
|
+
```rust
|
|
12
|
+
/// Returns the length of the buffer.
|
|
13
|
+
///
|
|
14
|
+
/// See also `capacity()` for the allocated size, and the
|
|
15
|
+
/// `Buffer` struct for more details.
|
|
16
|
+
pub fn len(&self) -> usize {
|
|
17
|
+
self.data.len()
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/// Parses the input using std::str::FromStr trait.
|
|
21
|
+
/// Check the Error enum for possible failures.
|
|
22
|
+
pub fn parse<T: FromStr>(input: &str) -> Result<T, Error> {
|
|
23
|
+
// ...
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Good
|
|
28
|
+
|
|
29
|
+
```rust
|
|
30
|
+
/// Returns the length of the buffer.
|
|
31
|
+
///
|
|
32
|
+
/// See also [`capacity()`](Self::capacity) for the allocated size, and
|
|
33
|
+
/// [`Buffer`] for more details.
|
|
34
|
+
pub fn len(&self) -> usize {
|
|
35
|
+
self.data.len()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// Parses the input using [`FromStr`] trait.
|
|
39
|
+
/// Check [`Error`] for possible failures.
|
|
40
|
+
///
|
|
41
|
+
/// [`FromStr`]: std::str::FromStr
|
|
42
|
+
pub fn parse<T: FromStr>(input: &str) -> Result<T, Error> {
|
|
43
|
+
// ...
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Link Syntax
|
|
48
|
+
|
|
49
|
+
| Syntax | Links To | Example |
|
|
50
|
+
|--------|----------|---------|
|
|
51
|
+
| `[Name]` | Item in scope | `[Vec]`, `[Option]` |
|
|
52
|
+
| `[path::Name]` | Fully qualified item | `[std::vec::Vec]` |
|
|
53
|
+
| `[Self::method]` | Method on current type | `[Self::new]` |
|
|
54
|
+
| `[Type::method]` | Method on other type | `[String::new]` |
|
|
55
|
+
| `[Type::CONST]` | Associated constant | `[usize::MAX]` |
|
|
56
|
+
| `[text](path)` | Custom text | `[see here](Self::len)` |
|
|
57
|
+
|
|
58
|
+
## Common Patterns
|
|
59
|
+
|
|
60
|
+
### Linking to Self Members
|
|
61
|
+
|
|
62
|
+
```rust
|
|
63
|
+
impl Buffer {
|
|
64
|
+
/// Creates an empty buffer.
|
|
65
|
+
///
|
|
66
|
+
/// Use [`with_capacity`](Self::with_capacity) if you know the size.
|
|
67
|
+
pub fn new() -> Self { /* ... */ }
|
|
68
|
+
|
|
69
|
+
/// Creates a buffer with pre-allocated capacity.
|
|
70
|
+
///
|
|
71
|
+
/// See [`new`](Self::new) for the default constructor.
|
|
72
|
+
pub fn with_capacity(cap: usize) -> Self { /* ... */ }
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Linking to Trait Methods
|
|
77
|
+
|
|
78
|
+
```rust
|
|
79
|
+
/// Converts to a string representation.
|
|
80
|
+
///
|
|
81
|
+
/// This is the implementation of [`Display::fmt`](std::fmt::Display::fmt).
|
|
82
|
+
impl Display for MyType {
|
|
83
|
+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
84
|
+
// ...
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Disambiguation
|
|
90
|
+
|
|
91
|
+
When names conflict, use suffixes:
|
|
92
|
+
|
|
93
|
+
```rust
|
|
94
|
+
/// See [`foo()`](fn@foo) for the function and [`foo`](mod@foo) for the module.
|
|
95
|
+
|
|
96
|
+
/// Works with [`Error`](struct@Error) struct or [`Error`](trait@Error) trait.
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
| Suffix | Item Type |
|
|
100
|
+
|--------|-----------|
|
|
101
|
+
| `fn@` | Function |
|
|
102
|
+
| `mod@` | Module |
|
|
103
|
+
| `struct@` | Struct |
|
|
104
|
+
| `enum@` | Enum |
|
|
105
|
+
| `trait@` | Trait |
|
|
106
|
+
| `type@` | Type alias |
|
|
107
|
+
| `const@` | Constant |
|
|
108
|
+
| `macro@` | Macro |
|
|
109
|
+
|
|
110
|
+
### Reference-Style Links
|
|
111
|
+
|
|
112
|
+
For repeated links or long paths:
|
|
113
|
+
|
|
114
|
+
```rust
|
|
115
|
+
/// Parses using [`serde`] with [`Deserialize`] trait.
|
|
116
|
+
/// Returns a [`Result`] that may contain [`Error`].
|
|
117
|
+
///
|
|
118
|
+
/// [`serde`]: https://serde.rs
|
|
119
|
+
/// [`Deserialize`]: serde::Deserialize
|
|
120
|
+
/// [`Result`]: std::result::Result
|
|
121
|
+
/// [`Error`]: crate::Error
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Verification
|
|
125
|
+
|
|
126
|
+
Enable link checking in CI:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
This fails if any intra-doc links are broken.
|
|
133
|
+
|
|
134
|
+
## See Also
|
|
135
|
+
|
|
136
|
+
- [doc-all-public](./doc-all-public.md) - Documenting public items
|
|
137
|
+
- [doc-examples-section](./doc-examples-section.md) - Adding examples
|
|
138
|
+
- [doc-errors-section](./doc-errors-section.md) - Documenting errors
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# doc-link-types
|
|
2
|
+
|
|
3
|
+
> Use intra-doc links to connect related types and functions
|
|
4
|
+
|
|
5
|
+
## Why It Matters
|
|
6
|
+
|
|
7
|
+
Intra-doc links (`[`TypeName`]`) create clickable references in generated documentation. They enable navigation between related items, verify that referenced items exist at compile time, and update automatically when items are renamed. Plain text references become stale and unclickable.
|
|
8
|
+
|
|
9
|
+
## Bad
|
|
10
|
+
|
|
11
|
+
```rust
|
|
12
|
+
/// Parses input and returns a ParseResult.
|
|
13
|
+
///
|
|
14
|
+
/// See also: ParseError for error types.
|
|
15
|
+
/// Uses the Tokenizer internally.
|
|
16
|
+
pub fn parse(input: &str) -> ParseResult {
|
|
17
|
+
// "ParseResult", "ParseError", "Tokenizer" are not clickable
|
|
18
|
+
// No verification they exist
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Good
|
|
23
|
+
|
|
24
|
+
```rust
|
|
25
|
+
/// Parses input and returns a [`ParseResult`].
|
|
26
|
+
///
|
|
27
|
+
/// # Errors
|
|
28
|
+
///
|
|
29
|
+
/// Returns [`ParseError::InvalidSyntax`] if the input contains invalid tokens.
|
|
30
|
+
/// Returns [`ParseError::UnexpectedEof`] if the input ends prematurely.
|
|
31
|
+
///
|
|
32
|
+
/// # Related
|
|
33
|
+
///
|
|
34
|
+
/// - [`Tokenizer`] - The underlying tokenizer used by this parser
|
|
35
|
+
/// - [`parse_file`] - Convenience function for parsing files
|
|
36
|
+
/// - [`ParseOptions`] - Configuration options for parsing
|
|
37
|
+
pub fn parse(input: &str) -> ParseResult {
|
|
38
|
+
// All links are clickable and verified
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Link Syntax
|
|
43
|
+
|
|
44
|
+
```rust
|
|
45
|
+
/// Basic link to type in same module
|
|
46
|
+
/// See [`MyType`] for details.
|
|
47
|
+
|
|
48
|
+
/// Link to method
|
|
49
|
+
/// Use [`MyType::new`] to create instances.
|
|
50
|
+
|
|
51
|
+
/// Link to associated type
|
|
52
|
+
/// Returns [`Iterator::Item`].
|
|
53
|
+
|
|
54
|
+
/// Link to module
|
|
55
|
+
/// See the [`parser`] module.
|
|
56
|
+
|
|
57
|
+
/// Link to external crate type
|
|
58
|
+
/// Works with [`std::collections::HashMap`].
|
|
59
|
+
|
|
60
|
+
/// Link with custom text
|
|
61
|
+
/// See [the parser][`parse`] for details.
|
|
62
|
+
|
|
63
|
+
/// Link to module item
|
|
64
|
+
/// See [`crate::utils::helper`].
|
|
65
|
+
|
|
66
|
+
/// Link to parent module item
|
|
67
|
+
/// See [`super::Parent`].
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Common Patterns
|
|
71
|
+
|
|
72
|
+
```rust
|
|
73
|
+
/// A configuration builder.
|
|
74
|
+
///
|
|
75
|
+
/// # Example
|
|
76
|
+
///
|
|
77
|
+
/// ```
|
|
78
|
+
/// use my_crate::Config;
|
|
79
|
+
///
|
|
80
|
+
/// let config = Config::builder()
|
|
81
|
+
/// .timeout(30)
|
|
82
|
+
/// .build()?;
|
|
83
|
+
/// ```
|
|
84
|
+
///
|
|
85
|
+
/// # Methods
|
|
86
|
+
///
|
|
87
|
+
/// - [`Config::builder`] - Create a new builder
|
|
88
|
+
/// - [`Config::default`] - Create with defaults
|
|
89
|
+
///
|
|
90
|
+
/// # Related Types
|
|
91
|
+
///
|
|
92
|
+
/// - [`ConfigBuilder`] - The builder returned by [`Config::builder`]
|
|
93
|
+
/// - [`ConfigError`] - Errors that can occur when building
|
|
94
|
+
pub struct Config { ... }
|
|
95
|
+
|
|
96
|
+
impl Config {
|
|
97
|
+
/// Creates a new [`ConfigBuilder`].
|
|
98
|
+
///
|
|
99
|
+
/// This is equivalent to [`ConfigBuilder::new`].
|
|
100
|
+
pub fn builder() -> ConfigBuilder { ... }
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Linking to Trait Items
|
|
105
|
+
|
|
106
|
+
```rust
|
|
107
|
+
/// Implements [`Iterator`] for lazy evaluation.
|
|
108
|
+
///
|
|
109
|
+
/// The [`Iterator::next`] method advances the cursor.
|
|
110
|
+
///
|
|
111
|
+
/// For parallel iteration, see [`rayon::ParallelIterator`].
|
|
112
|
+
pub struct MyIterator { ... }
|
|
113
|
+
|
|
114
|
+
impl Iterator for MyIterator {
|
|
115
|
+
/// Advances and returns the next value.
|
|
116
|
+
///
|
|
117
|
+
/// See also [`Iterator::nth`] for skipping elements.
|
|
118
|
+
fn next(&mut self) -> Option<Self::Item> { ... }
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Broken Link Detection
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
# Catch broken intra-doc links
|
|
126
|
+
RUSTDOCFLAGS="-D warnings" cargo doc
|
|
127
|
+
|
|
128
|
+
# Or in CI
|
|
129
|
+
cargo doc --no-deps 2>&1 | grep "warning: unresolved link"
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
```toml
|
|
133
|
+
# Cargo.toml - deny broken links
|
|
134
|
+
[lints.rustdoc]
|
|
135
|
+
broken_intra_doc_links = "deny"
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Module-Level Documentation
|
|
139
|
+
|
|
140
|
+
```rust
|
|
141
|
+
//! # Parser Module
|
|
142
|
+
//!
|
|
143
|
+
//! This module provides parsing utilities.
|
|
144
|
+
//!
|
|
145
|
+
//! ## Main Types
|
|
146
|
+
//!
|
|
147
|
+
//! - [`Parser`] - The main parser struct
|
|
148
|
+
//! - [`Token`] - Tokens produced by tokenization
|
|
149
|
+
//! - [`Ast`] - The abstract syntax tree
|
|
150
|
+
//!
|
|
151
|
+
//! ## Functions
|
|
152
|
+
//!
|
|
153
|
+
//! - [`parse`] - Parse a string
|
|
154
|
+
//! - [`parse_file`] - Parse a file
|
|
155
|
+
//!
|
|
156
|
+
//! ## Errors
|
|
157
|
+
//!
|
|
158
|
+
//! All functions return [`ParseError`] on failure.
|
|
159
|
+
|
|
160
|
+
pub struct Parser { ... }
|
|
161
|
+
pub enum Token { ... }
|
|
162
|
+
pub struct Ast { ... }
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## See Also
|
|
166
|
+
|
|
167
|
+
- [doc-examples-section](./doc-examples-section.md) - Code examples in docs
|
|
168
|
+
- [err-doc-errors](./err-doc-errors.md) - Documenting errors
|
|
169
|
+
- [lint-deny-correctness](./lint-deny-correctness.md) - Lint settings
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# doc-module-inner
|
|
2
|
+
|
|
3
|
+
> Use `//!` for module-level documentation
|
|
4
|
+
|
|
5
|
+
## Why It Matters
|
|
6
|
+
|
|
7
|
+
Inner doc comments (`//!`) document the module itself, not the next item. They appear at the top of module files and describe the module's purpose, contents, and usage patterns. This helps users understand what a module provides before diving into individual items.
|
|
8
|
+
|
|
9
|
+
Module docs are the first thing users see in `cargo doc` when navigating to a module.
|
|
10
|
+
|
|
11
|
+
## Bad
|
|
12
|
+
|
|
13
|
+
```rust
|
|
14
|
+
// This module handles authentication
|
|
15
|
+
// It provides JWT and session-based auth
|
|
16
|
+
|
|
17
|
+
mod auth;
|
|
18
|
+
|
|
19
|
+
pub use auth::*;
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```rust
|
|
23
|
+
// auth.rs
|
|
24
|
+
/// Authentication utilities // Wrong: this documents nothing useful
|
|
25
|
+
use std::collections::HashMap;
|
|
26
|
+
|
|
27
|
+
pub struct Session { /* ... */ }
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Good
|
|
31
|
+
|
|
32
|
+
```rust
|
|
33
|
+
//! Authentication and authorization utilities.
|
|
34
|
+
//!
|
|
35
|
+
//! This module provides multiple authentication strategies:
|
|
36
|
+
//!
|
|
37
|
+
//! - [`JwtAuth`] - JSON Web Token based authentication
|
|
38
|
+
//! - [`SessionAuth`] - Cookie-based session authentication
|
|
39
|
+
//! - [`ApiKeyAuth`] - API key authentication for services
|
|
40
|
+
//!
|
|
41
|
+
//! # Examples
|
|
42
|
+
//!
|
|
43
|
+
//! ```
|
|
44
|
+
//! use my_crate::auth::{JwtAuth, Authenticator};
|
|
45
|
+
//!
|
|
46
|
+
//! let auth = JwtAuth::new("secret-key");
|
|
47
|
+
//! let token = auth.generate_token(&user)?;
|
|
48
|
+
//! ```
|
|
49
|
+
//!
|
|
50
|
+
//! # Feature Flags
|
|
51
|
+
//!
|
|
52
|
+
//! - `jwt` - Enables JWT authentication (enabled by default)
|
|
53
|
+
//! - `sessions` - Enables session-based authentication
|
|
54
|
+
|
|
55
|
+
use std::collections::HashMap;
|
|
56
|
+
|
|
57
|
+
pub struct Session { /* ... */ }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Where to Use Inner Docs
|
|
61
|
+
|
|
62
|
+
| Location | Purpose |
|
|
63
|
+
|----------|---------|
|
|
64
|
+
| `lib.rs` | Crate-level documentation (appears on crate root) |
|
|
65
|
+
| `mod.rs` | Module documentation for directory modules |
|
|
66
|
+
| `module.rs` | Module documentation for single-file modules |
|
|
67
|
+
|
|
68
|
+
## Crate Root Example
|
|
69
|
+
|
|
70
|
+
```rust
|
|
71
|
+
//! # My Awesome Crate
|
|
72
|
+
//!
|
|
73
|
+
//! `my_crate` provides utilities for handling complex workflows.
|
|
74
|
+
//!
|
|
75
|
+
//! ## Quick Start
|
|
76
|
+
//!
|
|
77
|
+
//! ```rust
|
|
78
|
+
//! use my_crate::prelude::*;
|
|
79
|
+
//!
|
|
80
|
+
//! let workflow = Workflow::builder()
|
|
81
|
+
//! .add_step(Step::new("fetch"))
|
|
82
|
+
//! .add_step(Step::new("process"))
|
|
83
|
+
//! .build();
|
|
84
|
+
//! ```
|
|
85
|
+
//!
|
|
86
|
+
//! ## Modules
|
|
87
|
+
//!
|
|
88
|
+
//! - [`workflow`] - Core workflow engine
|
|
89
|
+
//! - [`steps`] - Built-in workflow steps
|
|
90
|
+
//! - [`prelude`] - Common imports
|
|
91
|
+
//!
|
|
92
|
+
//! ## Feature Flags
|
|
93
|
+
//!
|
|
94
|
+
//! | Feature | Description |
|
|
95
|
+
//! |---------|-------------|
|
|
96
|
+
//! | `async` | Async workflow execution |
|
|
97
|
+
//! | `serde` | Serialization support |
|
|
98
|
+
|
|
99
|
+
pub mod workflow;
|
|
100
|
+
pub mod steps;
|
|
101
|
+
pub mod prelude;
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Key Sections for Module Docs
|
|
105
|
+
|
|
106
|
+
1. **Brief description** - One-line summary
|
|
107
|
+
2. **Overview** - What the module provides
|
|
108
|
+
3. **Examples** - How to use it
|
|
109
|
+
4. **Feature flags** - Optional functionality
|
|
110
|
+
5. **See Also** - Related modules
|
|
111
|
+
|
|
112
|
+
## See Also
|
|
113
|
+
|
|
114
|
+
- [doc-all-public](./doc-all-public.md) - Documenting public items
|
|
115
|
+
- [doc-examples-section](./doc-examples-section.md) - Adding examples
|
|
116
|
+
- [doc-cargo-metadata](./doc-cargo-metadata.md) - Crate metadata
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# doc-panics-section
|
|
2
|
+
|
|
3
|
+
> Include `# Panics` section for functions that can panic
|
|
4
|
+
|
|
5
|
+
## Why It Matters
|
|
6
|
+
|
|
7
|
+
Panics are exceptional conditions that crash the program (or unwind the stack). Users need to know when a function might panic so they can ensure preconditions are met or avoid the function in contexts where panics are unacceptable (e.g., `no_std`, embedded, FFI).
|
|
8
|
+
|
|
9
|
+
If a function can panic, document exactly when.
|
|
10
|
+
|
|
11
|
+
## Bad
|
|
12
|
+
|
|
13
|
+
```rust
|
|
14
|
+
/// Returns the element at the given index.
|
|
15
|
+
pub fn get(index: usize) -> &T {
|
|
16
|
+
&self.data[index] // Panics if out of bounds - not documented!
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/// Divides two numbers.
|
|
20
|
+
pub fn divide(a: i32, b: i32) -> i32 {
|
|
21
|
+
a / b // Panics on division by zero - not documented!
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Good
|
|
26
|
+
|
|
27
|
+
```rust
|
|
28
|
+
/// Returns the element at the given index.
|
|
29
|
+
///
|
|
30
|
+
/// # Panics
|
|
31
|
+
///
|
|
32
|
+
/// Panics if `index` is out of bounds (i.e., `index >= self.len()`).
|
|
33
|
+
///
|
|
34
|
+
/// # Examples
|
|
35
|
+
///
|
|
36
|
+
/// ```
|
|
37
|
+
/// let v = vec![1, 2, 3];
|
|
38
|
+
/// assert_eq!(v.get(1), &2);
|
|
39
|
+
/// ```
|
|
40
|
+
pub fn get(&self, index: usize) -> &T {
|
|
41
|
+
&self.data[index]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// Divides two numbers.
|
|
45
|
+
///
|
|
46
|
+
/// # Panics
|
|
47
|
+
///
|
|
48
|
+
/// Panics if `divisor` is zero.
|
|
49
|
+
///
|
|
50
|
+
/// For a non-panicking version, use [`checked_divide`].
|
|
51
|
+
pub fn divide(dividend: i32, divisor: i32) -> i32 {
|
|
52
|
+
dividend / divisor
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Divides two numbers, returning `None` if the divisor is zero.
|
|
56
|
+
pub fn checked_divide(dividend: i32, divisor: i32) -> Option<i32> {
|
|
57
|
+
if divisor == 0 {
|
|
58
|
+
None
|
|
59
|
+
} else {
|
|
60
|
+
Some(dividend / divisor)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Common Panic Conditions
|
|
66
|
+
|
|
67
|
+
| Operation | Panic Condition |
|
|
68
|
+
|-----------|-----------------|
|
|
69
|
+
| Index access `[i]` | Index out of bounds |
|
|
70
|
+
| Division `/`, `%` | Division by zero |
|
|
71
|
+
| `.unwrap()` | `None` or `Err` value |
|
|
72
|
+
| `.expect()` | `None` or `Err` value |
|
|
73
|
+
| `slice::split_at(mid)` | `mid > len` |
|
|
74
|
+
| `Vec::remove(i)` | `i >= len` |
|
|
75
|
+
| Overflow (debug) | Integer overflow |
|
|
76
|
+
|
|
77
|
+
## Pattern: Panic vs Return Error
|
|
78
|
+
|
|
79
|
+
Document why you chose to panic vs return `Result`:
|
|
80
|
+
|
|
81
|
+
```rust
|
|
82
|
+
/// Creates a new buffer with the given capacity.
|
|
83
|
+
///
|
|
84
|
+
/// # Panics
|
|
85
|
+
///
|
|
86
|
+
/// Panics if `capacity` is zero. A buffer must have at least
|
|
87
|
+
/// one byte of capacity.
|
|
88
|
+
///
|
|
89
|
+
/// This panics rather than returning an error because a zero-capacity
|
|
90
|
+
/// buffer represents a programming error, not a runtime condition.
|
|
91
|
+
pub fn new(capacity: usize) -> Self {
|
|
92
|
+
assert!(capacity > 0, "capacity must be non-zero");
|
|
93
|
+
// ...
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Pattern: Debug-Only Panics
|
|
98
|
+
|
|
99
|
+
```rust
|
|
100
|
+
/// Adds an item to the collection.
|
|
101
|
+
///
|
|
102
|
+
/// # Panics
|
|
103
|
+
///
|
|
104
|
+
/// In debug builds, panics if the collection is at capacity.
|
|
105
|
+
/// In release builds, this is a no-op when at capacity.
|
|
106
|
+
pub fn push(&mut self, item: T) {
|
|
107
|
+
debug_assert!(self.len < self.capacity, "collection at capacity");
|
|
108
|
+
// ...
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Provide Non-Panicking Alternatives
|
|
113
|
+
|
|
114
|
+
When documenting a panicking function, point to safe alternatives:
|
|
115
|
+
|
|
116
|
+
```rust
|
|
117
|
+
/// # Panics
|
|
118
|
+
///
|
|
119
|
+
/// Panics if the index is out of bounds.
|
|
120
|
+
///
|
|
121
|
+
/// For a non-panicking version, use [`get`] which returns `Option<&T>`.
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## See Also
|
|
125
|
+
|
|
126
|
+
- [doc-errors-section](./doc-errors-section.md) - Documenting errors
|
|
127
|
+
- [doc-safety-section](./doc-safety-section.md) - Documenting unsafe
|
|
128
|
+
- [err-result-over-panic](./err-result-over-panic.md) - Preferring Result
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# doc-question-mark
|
|
2
|
+
|
|
3
|
+
> Use `?` in examples, not `.unwrap()`
|
|
4
|
+
|
|
5
|
+
## Why It Matters
|
|
6
|
+
|
|
7
|
+
Doc examples should model best practices. Using `.unwrap()` teaches users to ignore errors, while `?` demonstrates proper error propagation. Examples with `?` also fail the doctest if an error occurs, catching bugs in documentation.
|
|
8
|
+
|
|
9
|
+
Rust doctests wrap examples in a function that returns `Result<(), E>` by default when you use `?`, making this pattern easy to adopt.
|
|
10
|
+
|
|
11
|
+
## Bad
|
|
12
|
+
|
|
13
|
+
```rust
|
|
14
|
+
/// Reads a configuration file.
|
|
15
|
+
///
|
|
16
|
+
/// # Examples
|
|
17
|
+
///
|
|
18
|
+
/// ```
|
|
19
|
+
/// let config = Config::from_file("config.toml").unwrap();
|
|
20
|
+
/// println!("{:?}", config.database_url);
|
|
21
|
+
/// ```
|
|
22
|
+
pub fn from_file(path: &str) -> Result<Config, Error> {
|
|
23
|
+
// ...
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// Fetches data from the API.
|
|
27
|
+
///
|
|
28
|
+
/// # Examples
|
|
29
|
+
///
|
|
30
|
+
/// ```
|
|
31
|
+
/// let client = Client::new();
|
|
32
|
+
/// let response = client.get("https://api.example.com").unwrap();
|
|
33
|
+
/// let data: Data = response.json().unwrap();
|
|
34
|
+
/// ```
|
|
35
|
+
pub async fn get(&self, url: &str) -> Result<Response, Error> {
|
|
36
|
+
// ...
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Good
|
|
41
|
+
|
|
42
|
+
```rust
|
|
43
|
+
/// Reads a configuration file.
|
|
44
|
+
///
|
|
45
|
+
/// # Examples
|
|
46
|
+
///
|
|
47
|
+
/// ```
|
|
48
|
+
/// # use my_crate::{Config, Error};
|
|
49
|
+
/// # fn main() -> Result<(), Error> {
|
|
50
|
+
/// let config = Config::from_file("config.toml")?;
|
|
51
|
+
/// println!("{:?}", config.database_url);
|
|
52
|
+
/// # Ok(())
|
|
53
|
+
/// # }
|
|
54
|
+
/// ```
|
|
55
|
+
pub fn from_file(path: &str) -> Result<Config, Error> {
|
|
56
|
+
// ...
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// Fetches data from the API.
|
|
60
|
+
///
|
|
61
|
+
/// # Examples
|
|
62
|
+
///
|
|
63
|
+
/// ```no_run
|
|
64
|
+
/// # use my_crate::{Client, Data, Error};
|
|
65
|
+
/// # async fn example() -> Result<(), Error> {
|
|
66
|
+
/// let client = Client::new();
|
|
67
|
+
/// let response = client.get("https://api.example.com").await?;
|
|
68
|
+
/// let data: Data = response.json().await?;
|
|
69
|
+
/// # Ok(())
|
|
70
|
+
/// # }
|
|
71
|
+
/// ```
|
|
72
|
+
pub async fn get(&self, url: &str) -> Result<Response, Error> {
|
|
73
|
+
// ...
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Doctest Wrapper Pattern
|
|
78
|
+
|
|
79
|
+
Rust wraps doc examples in a function. You can make this explicit:
|
|
80
|
+
|
|
81
|
+
```rust
|
|
82
|
+
/// # Examples
|
|
83
|
+
///
|
|
84
|
+
/// ```
|
|
85
|
+
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
86
|
+
/// let value = parse_config("key=value")?;
|
|
87
|
+
/// assert_eq!(value.key, "value");
|
|
88
|
+
/// # Ok(())
|
|
89
|
+
/// # }
|
|
90
|
+
/// ```
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Or use the implicit wrapper (Rust 2021+):
|
|
94
|
+
|
|
95
|
+
```rust
|
|
96
|
+
/// # Examples
|
|
97
|
+
///
|
|
98
|
+
/// ```
|
|
99
|
+
/// # use my_crate::parse_config;
|
|
100
|
+
/// let value = parse_config("key=value")?;
|
|
101
|
+
/// assert_eq!(value.key, "value");
|
|
102
|
+
/// # Ok::<(), my_crate::Error>(())
|
|
103
|
+
/// ```
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## When to Use `.unwrap()`
|
|
107
|
+
|
|
108
|
+
There are specific cases where `.unwrap()` is acceptable in examples:
|
|
109
|
+
|
|
110
|
+
```rust
|
|
111
|
+
/// # Examples
|
|
112
|
+
///
|
|
113
|
+
/// ```
|
|
114
|
+
/// // Static regex that is known at compile time to be valid
|
|
115
|
+
/// let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
|
|
116
|
+
///
|
|
117
|
+
/// // Parsing a literal that cannot fail
|
|
118
|
+
/// let n: i32 = "42".parse().unwrap();
|
|
119
|
+
/// ```
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
But still prefer `?` when demonstrating error handling patterns.
|
|
123
|
+
|
|
124
|
+
## Comparison
|
|
125
|
+
|
|
126
|
+
| Pattern | Behavior on Error | Teaches |
|
|
127
|
+
|---------|-------------------|---------|
|
|
128
|
+
| `.unwrap()` | Panics with generic message | Bad habits |
|
|
129
|
+
| `.expect()` | Panics with custom message | Slightly better |
|
|
130
|
+
| `?` | Propagates error, test fails | Best practices |
|
|
131
|
+
|
|
132
|
+
## See Also
|
|
133
|
+
|
|
134
|
+
- [doc-examples-section](./doc-examples-section.md) - Writing examples
|
|
135
|
+
- [doc-hidden-setup](./doc-hidden-setup.md) - Hiding setup code
|
|
136
|
+
- [err-question-mark](./err-question-mark.md) - Error propagation
|