Skip to content

Commit a65d48f

Browse files
committed
Add a section on inferred generic arguments
This adds some rules explaining the basics of this. Of course the actual inference algorithm should eventually be described.
1 parent 6d50db0 commit a65d48f

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/types/generics/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,26 @@ FCW exists for non-value (function) position, see
227227
https://doc.rust-lang.org/nightly/rustc/lints/listing/warn-by-default.html#late-bound-lifetime-arguments
228228
-->
229229
230+
r[generics.arguments.inference]
231+
### Infer arguments
232+
233+
r[generics.arguments.inference.intro]
234+
The placeholder `_` may be used for type or const arguments when the compiler can infer the value.
235+
236+
> [!EXAMPLE]
237+
> ```rust
238+
> let v: Vec<_> = vec![1, 2, 3]; // _ inferred as i32
239+
>
240+
> type T<const N: usize> = [i32; N];
241+
> let x: T<_> = [1]; // _ inferred as 1
242+
> ```
243+
244+
> [!NOTE]
245+
> The `_` placeholder cannot be used for lifetime arguments; use `'_` for elided lifetimes instead.
246+
247+
r[generics.arguments.inference.parentheses]
248+
Infer arguments may be surrounded by any number of matching parentheses.
249+
230250
r[generics.const]
231251
## Const generics
232252

0 commit comments

Comments
 (0)