From 7fd47ef4786f86ccdb5d6f0f198a6a9fdec5497c Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 10 Jan 2021 12:19:27 -0800 Subject: [PATCH] Clarify when const parameters need to be used. --- src/items/generics.md | 8 ++++++-- src/items/implementations.md | 12 +++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/items/generics.md b/src/items/generics.md index 8707c99ef..a7de436ec 100644 --- a/src/items/generics.md +++ b/src/items/generics.md @@ -171,8 +171,9 @@ fn foo() -> Foo { todo!() } // ERROR fn bar() -> Foo<{ N }> { todo!() } // ok ``` -Unlike type and lifetime parameters, const parameters of types can be used without -being mentioned inside of a parameterized type: +Unlike type and lifetime parameters, const parameters can be declared without +being used inside of a parameterized item, with the exception of +implementations as described in [generic implementations]: ```rust,compile_fail // ok @@ -182,6 +183,8 @@ enum Bar { A, B } // ERROR: unused parameter struct Baz; struct Biz<'a>; +struct Unconstrained; +impl Unconstrained {} ``` When resolving a trait bound obligation, the exhaustiveness of all @@ -292,6 +295,7 @@ struct Foo<#[my_flexible_clone(unbounded)] H> { [enumerations]: enumerations.md [functions]: functions.md [function pointers]: ../types/function-pointer.md +[generic implementations]: implementations.md#generic-implementations [higher-ranked lifetimes]: ../trait-bounds.md#higher-ranked-trait-bounds [implementations]: implementations.md [item declarations]: ../statements.md#item-declarations diff --git a/src/items/implementations.md b/src/items/implementations.md index 976241481..33f7c84d9 100644 --- a/src/items/implementations.md +++ b/src/items/implementations.md @@ -180,11 +180,11 @@ is considered local. ## Generic Implementations -An implementation can take type and lifetime parameters, which can be used in -the rest of the implementation. Type parameters declared for an implementation -must be used at least once in either the trait or the implementing type of an -implementation. Implementation parameters are written directly after the `impl` -keyword. +An implementation can take [generic parameters] which are written directly +after the `impl` keyword. The parameters can be used in the rest of the +implementation. Type and const parameters must be used at least once in either +the trait or the implementing type of an implementation. Lifetime parameters +do not need to be used unless they appear in an [associated type]. ```rust # trait Seq { fn dummy(&self, _: T) { } } @@ -219,6 +219,7 @@ attributes]. [trait]: traits.md [associated functions]: associated-items.md#associated-functions-and-methods [associated constants]: associated-items.md#associated-constants +[associated type]: associated-items.md#associated-types [attributes]: ../attributes.md [`cfg`]: ../conditional-compilation.md [`deprecated`]: ../attributes/diagnostics.md#the-deprecated-attribute @@ -230,3 +231,4 @@ attributes]. [local type]: ../glossary.md#local-type [fundamental types]: ../glossary.md#fundamental-type-constructors [uncovered type]: ../glossary.md#uncovered-type +[generic parameters]: generics.md