What does <T> mean anyways?

i have nagging problem with the Rust book or any tutorial for Rust... while introducing the language, they all suddenly come up with terms or terminologies in sample codes that are literally greek to newbies.

for example, what does

 <T>

mean as an argument type?? is it just a place holder like {} is?

Even though something like Vec<T> is mentioned earlier, the exact meaning of this syntax is properly explained only in chapter 10 of the book. Are you this far yet?

The relevant subchapter is 10.1. The first occurrence of <T> on that page also comes with detailed explanations:

To parameterize the types in a new single function, we need to name the type parameter, just as we do for the value parameters to a function. You can use any identifier as a type parameter name. But we’ll use T because, by convention, type parameter names in Rust are short, often just a letter, and Rust’s type-naming convention is CamelCase. Short for “type,” T is the default choice of most Rust programmers.

When we use a parameter in the body of the function, we have to declare the parameter name in the signature so the compiler knows what that name means. Similarly, when we use a type parameter name in a function signature, we have to declare the type parameter name before we use it. To define the generic largest function, place type name declarations inside angle brackets, <>, between the name of the function and the parameter list, like this:

fn largest<T>(list: &[T]) -> &T {

We read this definition as: the function largest is generic over some type T. This function has one parameter named list, which is a slice of values of type T. The largest function will return a reference to a value of the same type T.

A similar function signature is fn foo<T>(list: Vec<T>) which has multiple <T>s even, and further down the page there is the example

struct Point<T> {
    x: T,
    y: T,
}

impl<T> Point<T> {
    fn x(&self) -> &T {
        &self.x
    }
}

which shows generics in structs and impl blocks. The meaning of <T> varies on context. Either it introduces a generic type parameter (as in fn largest<T>, struct Point<T> or impl<T>), or it applies it (as in Vec<T> in the fn foo function signature above, or Point<T> in the impl block above).

As mentioned in the book, the name T is arbitrary and you could rename it; usually names starting with capital letters (in “CamelCase”, as the except from the book, too, explains above) are used, otherwise the compiler will warn you. So just like

let t = 42;
f(t);

is the same as

let foo = 42;
f(foo);

we can rewrite

fn largest<T>(list: &[T]) -> &T {
    …
}

just as well as

fn largest<Foo>(list: &[Foo]) -> &Foo {
    …
}
3 Likes

That's why we need tutorials in the first place. If you are ready to spend couple of months studying various things step-by-step without the ability to write and run anything then reference guide is always there.

Unfortunately practice shows that very few newbies have the ability to survive this approach: they become bored and leave way before they become able to write anything in Rust.

Tutorials, instead, use “trust me, you'll understand it later” approach which makes it feasible to just type things without understanding them and do small experiments before you have digested the whole Rust reference guide.

1 Like

no not yet as i'm stuck with this symbols' meaning. progress is slow.

as i see it, its a place holder then like { } is in println!().

like eating a whole cow :grin:

still digesting even after a month. slow?

It's placeholder, but a bit different from {}. It's called generic programming and lots of things are built on top of it in Rust.

Depends on how much programming experience do you have. If Rust is your first language then no, month is not slow, if that's your tenth language… then you wouldn't be asking what <T> means, I guess.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.