DAY 9
0

## Compound Types

Compound types 可以把很多的值合併再一起變成一個型態。Rust 有兩個主要的 compound types 分別是 tuples 以及 arrays.

### The Tuple Type

tuple 是一般常用來組合型態的方法他可以自己定義不同的資料類型放在裡面，這邊要注意的是 tuple 一但宣告之後就不能對他做任何的增減項目的調整。

``````fn compound_types() {
// 組合不同的型態成為一個單元
let tup: (i32, f64, u8) = (500, 6.4, 1);
}
``````

``````fn compound_types() {
// 組合不同的型態成為一個單元
let tup: (i32, f64, u8) = (500, 6.4, 1);
// 解構
let (x, y, z) = tup;
println!("The value of x is: {}", x);
println!("The value of y is: {}", y);
println!("The value of z is: {}", z);
}
``````

``````fn compound_types() {
// 組合不同的型態成為一個單元
let tup: (i32, f64, u8) = (500, 6.4, 1);
// 解構
println!("The value of tup.0 is: {}", tup.0);
println!("The value of tup.1 is: {}", tup.1);
println!("The value of tup.2 is: {}", tup.2);
}
``````

#### Tuple with mixed data type

``````const GLOBAL_VALUE: &str = "發大財";
static CHANGABLE_GLOBAL_VALUE: f64 = 10.5;

fn tuple_with_mixed_data_type() {
let tup: (&str, f64, u8) = (GLOBAL_VALUE, CHANGABLE_GLOBAL_VALUE, 1);
// 解構
println!("The value of tup.0 is: {}", tup.0); // 發大財
println!("The value of tup.1 is: {}", tup.1); // 10.5
println!("The value of tup.2 is: {}", tup.2); // 1
}
``````

``````const GLOBAL_VALUE: &str = "發大財";
static mut CHANGABLE_GLOBAL_VALUE: f64 = 10.5;

fn tuple_with_mixed_data_type() {
let tup: (&str, f64, u8) = (GLOBAL_VALUE, CHANGABLE_GLOBAL_VALUE, 1);
^^^^^^^^^^^^^^^^^^^^^^ use of mutable static
}
``````

``````const GLOBAL_VALUE: &str = "發大財";
static CHANGABLE_GLOBAL_VALUE: f64 = 10.5;

fn tuple_with_mixed_data_type() {
let tup: (&str, f64, u8) = (GLOBAL_VALUE, CHANGABLE_GLOBAL_VALUE, 1);
let tup = ("挖石油", 22.0, 92);
// 解構
println!("The value of tup.0 is: {}", tup.0); // 挖石油
println!("The value of tup.1 is: {}", tup.1); // 22
println!("The value of tup.2 is: {}", tup.2); // 92
}
``````

``````const GLOBAL_VALUE: &str = "發大財";
static CHANGABLE_GLOBAL_VALUE: f64 = 10.5;

pub fn tuple_with_mixed_data_type() {
let mut tup: (&str, f64, u8) = (GLOBAL_VALUE, CHANGABLE_GLOBAL_VALUE, 1);
tup.1 = 22.5;

// 解構
println!("The value of tup.0 is: {}", tup.0);
println!("The value of tup.1 is: {}", tup.1);
println!("The value of tup.2 is: {}", tup.2);
}
``````

mutable 的 tuple 就可以改變裡面任一的值。

### The Array Type

Rust 的 array 裡面的 item 型態必須都一樣，因此不像 javascript 要在裡面放什麼都可以。

``````fn compound_types() {
// 陣列裡的物件必須都為同樣的類型
let months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
}
``````

``````fn compound_types() {
let a: [i32; 5] = [1, 2, 3, 4, 5];
}
``````

i32 是 array 物件的類型 5 則代表 array 的容量。

``````fn compound_types() {
let a = [3; 5];
// 等同於 let a = [3, 3, 3, 3, 3];
}
``````

``````fn compound_types() {
let a = [1, 2, 3, 4, 5];

let first = a[0];
let second = a[1];
let third = a[10];
^^^^^ index out of bounds
}
``````

`Note：若 index 如果超出 array 長度時在編譯時會直接報錯。`

``````fn compound_types() {
let a = [1, 2, 3, 4, 5];

let first = a[0];
let second = a[1];
let index = 5;
let third = a[index];
}
``````

``````fn compound_types() {
let a = [1, 2, 3, 4, 5];

let first = a[0];
let second = a[1];
a[0] = 5;
^^^^^^^^ cannot assign
}
``````

``````fn compound_types() {
let mut a = [1, 2, 3, 4, 5];

let first = a[0];
let second = a[1];
a[0] = 5;
}
``````