When we write:
fn foo<'a, 'b>(x: &'a u32, y: &'b u32) -> &'a u32 {
x
}
Why don't we just refer to the 'a
s and the 'b
s as lifetime parameters as opposed to generic lifetime parameters? It's just a syntactical way to communicate to the compiler a constraint on the returned reference's lifetime in terms of the lifetimes of the arguments. I'm struggling to see the rationale for the inclusion of the word "generic" here.
You can think of "generic type" as a shorthand for "type with generic parameters", but those generic parameters can be any or all of:
generic type parameters (as in
Vec<T>
);generic lifetime parameters (as in
type Ref<'a> = &'a i32
);generic const parameters (real soon now).
Same for "generic function" = "function with generic parameters".
You could shorten it to "lifetime parameters" exactly because all lifetime parameters (not all lifetimes!) are generic. The same applies to type parameters.
Lifetime parameters and type parameters have a few things in common which don't have to go together, but do in Rust:
the most obvious: they are written between
<
>
and not(
)
;they can be inferred;
code can't branch depending on their value.
So they are grouped together as "generic parameters" as opposed to "value parameters".