O'Reilly logo

Rust Essentials by Ivo Balbaert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generic data structures and functions

Genericity is the capacity to write code once, without or partly specified types, so that the code can be used for many different types. Rust has this capacity in abundance and applies it for both data structures and functions.

A composite data structure is generic if the type of its items can be of a general <T> type. T can be an i32, an f64, a String, or a struct type such as Person that we coded ourselves. So, we can have not only Vec<f64> but also Vec<Person>. If you make T a concrete type, then you must substitute T with that type everywhere T appears in the definition of the data structure.

Our data structure can be parametrized with a generic <T> type, so it has multiple concrete definitions—it is polymorphic. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required