Skip to content

Commit 7d86a9e

Browse files
committed
Clarify when const parameters need to be used.
1 parent 85a363c commit 7d86a9e

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/items/generics.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ fn foo<const N: usize>() -> Foo<N> { todo!() } // ERROR
181181
fn bar<const N: usize>() -> Foo<{ N }> { todo!() } // ok
182182
```
183183

184-
Unlike type and lifetime parameters, const parameters of types can be used without
185-
being mentioned inside of a parameterized type:
184+
Unlike type and lifetime parameters, const parameters can be declared without
185+
being used inside of a parameterized item, with the exception of
186+
implementations as described in [generic implementations]:
186187

187188
```rust,compile_fail
188189
// ok
@@ -192,6 +193,8 @@ enum Bar<const M: usize> { A, B }
192193
// ERROR: unused parameter
193194
struct Baz<T>;
194195
struct Biz<'a>;
196+
struct Unconstrained;
197+
impl<const N: usize> Unconstrained {}
195198
```
196199

197200
When resolving a trait bound obligation, the exhaustiveness of all
@@ -298,6 +301,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> {
298301
[enumerations]: enumerations.md
299302
[functions]: functions.md
300303
[function pointers]: ../types/function-pointer.md
304+
[generic implementations]: implementations.md#generic-implementations
301305
[higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds
302306
[implementations]: implementations.md
303307
[item declarations]: ../statements.md#item-declarations

src/items/implementations.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ is considered local.
180180

181181
## Generic Implementations
182182

183-
An implementation can take type and lifetime parameters, which can be used in
184-
the rest of the implementation. Type parameters declared for an implementation
185-
must be used at least once in either the trait or the implementing type of an
186-
implementation. Implementation parameters are written directly after the `impl`
187-
keyword.
183+
An implementation can take [generic parameters] which are written directly
184+
after the `impl` keyword. The parameters can be used in the rest of the
185+
implementation. Type and const parameters must be used at least once in either
186+
the trait or the implementing type of an implementation. Lifetime parameters
187+
do not need to be used unless they appear in an [associated type].
188188

189189
```rust
190190
# trait Seq<T> { fn dummy(&self, _: T) { } }
@@ -219,6 +219,7 @@ attributes].
219219
[trait]: traits.md
220220
[associated functions]: associated-items.md#associated-functions-and-methods
221221
[associated constants]: associated-items.md#associated-constants
222+
[associated type]: associated-items.md#associated-types
222223
[attributes]: ../attributes.md
223224
[`cfg`]: ../conditional-compilation.md
224225
[`deprecated`]: ../attributes/diagnostics.md#the-deprecated-attribute
@@ -230,3 +231,4 @@ attributes].
230231
[local type]: ../glossary.md#local-type
231232
[fundamental types]: ../glossary.md#fundamental-type-constructors
232233
[uncovered type]: ../glossary.md#uncovered-type
234+
[generic parameters]: generics.md

0 commit comments

Comments
 (0)