Generic parameters
Syntax
GenericParams :
<
>
|<
(GenericParam,
)* GenericParam,
?>
GenericParam :
OuterAttribute* ( LifetimeParam | TypeParam | ConstParam )LifetimeParam :
LIFETIME_OR_LABEL (:
LifetimeBounds )?TypeParam :
IDENTIFIER(:
TypeParamBounds? )? (=
Type )?ConstParam:
const
IDENTIFIER:
Type (=
Block | IDENTIFIER | -?LITERAL )?
Functions, type aliases, structs, enumerations, unions, traits, and
implementations may be parameterized by types, constants, and lifetimes. These
parameters are listed in angle brackets (<...>
),
usually immediately after the name of the item and before its definition. For
implementations, which don't have a name, they come directly after impl
.
The order of generic parameters is restricted to lifetime parameters and then type and const parameters intermixed.
Some examples of items with type, const, and lifetime parameters:
Generic parameters are in scope within the item definition where they are declared. They are not in scope for items declared within the body of a function as described in item declarations.
References, raw pointers, arrays, slices, tuples, and function pointers have lifetime or type parameters as well, but are not referred to with path syntax.
Const generics
Const generic parameters allow items to be generic over constant values. The const identifier introduces a name for the constant parameter, and all instances of the item must be instantiated with a value of the given type.
The only allowed types of const parameters are u8
, u16
, u32
, u64
, u128
, usize
,
i8
, i16
, i32
, i64
, i128
, isize
, char
and bool
.
Const parameters can be used anywhere a const item can be used, with the exception that when used in a type or array repeat expression, it must be standalone (as described below). That is, they are allowed in the following places:
- As an applied const to any type which forms a part of the signature of the item in question.
- As part of a const expression used to define an associated const, or as a parameter to an associated type.
- As a value in any runtime expression in the body of any functions in the item.
- As a parameter to any type used in the body of any functions in the item.
- As a part of the type of any fields in the item.
As a further restriction, const parameters may only appear as a standalone
argument inside of a type or array repeat expression. In those contexts,
they may only be used as a single segment path expression, possibly inside a
block (such as N
or {N}
). That is, they cannot be combined with other
expressions.
A const argument in a path specifies the const value to use for that item.
The argument must be a const expression of the type ascribed to the const
parameter. The const expression must be a block expression
(surrounded with braces) unless it is a single path segment (an IDENTIFIER)
or a literal (with a possibly leading -
token).
Note: This syntactic restriction is necessary to avoid requiring infinite lookahead when parsing an expression inside of a type.
When there is ambiguity if a generic argument could be resolved as either a type or const argument, it is always resolved as a type. Placing the argument in a block expression can force it to be interpreted as a const argument.
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:
When resolving a trait bound obligation, the exhaustiveness of all
implementations of const parameters is not considered when determining if the
bound is satisfied. For example, in the following, even though all possible
const values for the bool
type are implemented, it is still an error that
the trait bound is not satisfied:
Where clauses
Syntax
WhereClause :
where
( WhereClauseItem,
)* WhereClauseItem ?WhereClauseItem :
LifetimeWhereClauseItem
| TypeBoundWhereClauseItemLifetimeWhereClauseItem :
Lifetime:
LifetimeBoundsTypeBoundWhereClauseItem :
ForLifetimes? Type:
TypeParamBounds?
Where clauses provide another way to specify bounds on type and lifetime parameters as well as a way to specify bounds on types that aren't type parameters.
The for
keyword can be used to introduce higher-ranked lifetimes. It only
allows LifetimeParam parameters.
Attributes
Generic lifetime and type parameters allow attributes on them. There are no built-in attributes that do anything in this position, although custom derive attributes may give meaning to it.
This example shows using a custom derive attribute to modify the meaning of a generic parameter.
// Assume that the derive for MyFlexibleClone declared `my_flexible_clone` as
// an attribute it understands.
#[derive(MyFlexibleClone)]
struct Foo<#[my_flexible_clone(unbounded)] H> {
a: *const H
}