© Carlo Milanesi 2018
Carlo MilanesiBeginning Rusthttps://doi.org/10.1007/978-1-4842-3468-6_8

8. Using Heterogeneous Data Structures

Carlo Milanesi1 
(1)
Bergamo, Italy
 
In this chapter you will learn how to define and use other composite types:
  • Tuples

  • Structs

  • Tuple-Structs

They are useful to group objects of different types.

At the end of the chapter, you’ll see some code style conventions.

The Tuples

Arrays and vectors can contain several items, yet such items must all be of the same type. If you wish to store in a single object several sub-objects of different types, you can do in this way:

let data = (10000000, 183.19, 'Q');
let copy_of_data = data;
print!("{}, {}, {}",
    data.0, copy_of_data.1, data.2);

This will print: "10000000, 183.19, Q" ...

Get Beginning Rust: From Novice to Professional now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.