O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

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

Compound data

For defining custom data types, there are structs. The simpler form is called a tuple struct, where the individual fields are not named but are referred to by their position. This should mostly be used when your data consists of only one or a few fields to achieve better levels of type safety, such as here:

// tuplestruct.rs #[derive(PartialEq)]struct Fahrenheit(i64);#[derive(PartialEq)]struct Celsius(i64);fn main() { let temperature1 = Fahrenheit(10); let temperature2 = Celsius(10); println!("Is temperature 1 the same as temperature 2? Answer: {}",            temperature1 == temperature2); println!("Temperature 1 is {} fahrenheit", temperature1.0); println!("Temperature 2 is {} celsius", temperature2.0);}

What is inside the tuple struct ...

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