Using Trait Objects That Allow for Values of Different Types - The Rust Programming Language
In Chapter 8, we mentioned that one limitation of vectors is that they can store elements of only one type. We created a workaround in Listing 8-9 where we defined a SpreadsheetCell enum that had variants to hold integers, floats, and text. This meant we could store different types of data in each cell and still have a vector that represented a row of cells. This is a perfectly good solution when our interchangeable items are a fixed set of types that we know when our code is compiled. However, sometimes we want our library user to be able to extend the set of types that are valid in a particular situation. To show how we might achieve this, we’ll create an example graphical user interface (GUI) tool that iterates through a list of items, calling a draw method on each one to draw it to the screen—a common technique for GUI tools. We’ll create a library crate called gui that contains the structure of a GUI library. This crate might include some types for people to use, such as Button or
Redirecting... Redirecting to... ch18-02-trait-objects.html .
Explore this link on the map →related reading
- Storing Lists of Values with Vectors - The Rust Programming Languagedoc.rust-lang.org
- A half-hour to learn Rustfasterthanli.me
- Defining Shared Behavior with Traits - The Rust Programming Languagedoc.rust-lang.org
- Defining an Enum - The Rust Programming Languagedoc.rust-lang.org
- Rust Language Cheat Sheetcheats.rs
- Idea Listjzhao.xyz
- The Better Alternative to Lifetime GATs - Sabrina Jewsonsabrinajewson.org
- Generic Data Types - The Rust Programming Languagedoc.rust-lang.org
- Data Types - The Rust Programming Languagedoc.rust-lang.org
- Programming a Guessing Game - The Rust Programming Languagedoc.rust-lang.org
- Generic Types, Traits, and Lifetimes - The Rust Programming Languagedoc.rust-lang.org
- Variance - Rust Compiler Development Guiderustc-dev-guide.rust-lang.org