How to use one character generics

I'm really confused after reading several documentaries about one character generics. If I understand it right I can use them to simplify long names of argument types. But there are data types that do not work and now I'm confused on how to use them correctly, when, and where.

I don't understand what you're asking about. "One character generics" sounds like you're talking about, for example, the T in Box<T>. But then you say...

...and I have no idea what that's in reference to.

Could you please be more specific about what you're asking about, and what you don't understand?

Yes, I'm talking about <T> (as far as I know they called generics) and I don't understand how to use this. What are these for and when do I need them?

These are type parameters, which will be instantiated (substituted with a concrete type) when you call a generic function or instantiate a generic type.

They have nothing to do with "abbreviating" anything. They are not a syntactic device. Generic programming is a whole concept and discipline on its own.

And when do I need this? Or is this a complete optional field?

Any time you don't want to duplicate code for many concrete types (eg. collections), for example. You may want to google "generic programming" if the whole concept is completely unfamiliar to you.

Realistically, generics in Rust are not optional. You need to understand generics the moment you instantiate a Vec.

1 Like

Ok, thanks.

These are covered in the Generic Types, Traits, and Lifetimes section of the Rust Book.

7 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.