Tuples

Here are two ways to initialize a tuple:

let tup = (3, "foo"); 
let tup: (i32, &str) = (3, "foo"); 
 

On the first line, we let local type inference work and just declare what is inside the tuple. Rust will figure out the types. On the second line, we declare the types explicitly.

We can have as many (or as few) elements in the list as they are, in fact, an ordered list of a fixed size.

As with other variable types, we can assign one tuple to equal another as long as they contain the same types and number of parameters (arity). For example, the following have the same types and arity and so can be used to assign to each other:

let mut change = (1.1f32, 1); 
let into = (3.14f32, 6); 
change = into; 

The following wouldn't be allowed as the types don't match, even though the arity does:

let mut change = (1.1f32, 1); 
let into = (1, 3.14f32); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset