Rust Note(9)_Struct_implement

struct & struct implement

用於組合並儲存不同類型的資料。

有別於 Array, Vector 只能存放同類型的資料,struct 提供了更加彈性的組合。

透過組合用法,可以達成類似其他語言,比如說 Python or Javascript 一樣,使用類似 class 的概念。

[struct] 結構


使用關鍵字 ”struct” 來建立一個結構,並且指定型態。

程式碼

1
2
3
4
struct Rectangle {
width: u64,
height: u64
}

Working Flow

  • struct 關鍵字,開始建立結構
  • 賦予結構名稱,首字母需大寫
  • 建立的結構值在 {.........} 大括號這個作用域(scope)裡面。
    • 賦予結構值名稱
    • 指定結構值型態 : type: 賦予的意思
    • 不同結構值,中間以 , 隔開

struct 初始化


透過初始化 struct ,來達成使用 struct 的變數。

程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Rectangle {
width: u64,
height: u64
}

fn main() {
let rectangle_1 = Rectangle {
width: 80,
height: 30
};

println!("Rectangle 1 width is {}; height is {}.",
rectangle_1.width, rectangle_1.height)

}

//output: Rectangle 1 width is 80; height is 30.

Working Flow

  • 使用let 來初始化 struct.
    • 等號右邊必須要先跟 rust 說,你是要使用哪個 struct
    • 使用 {...} 包起來
    • 在 struct 裡面是指定型態,這邊就是指定值。
    • 各個值中間以, 隔開
    • 最後要記得加上 ; 分隔符。
    • 數值的型態如果沒有符合 struct 定義的,就會報錯。
      • ex: struct 宣告 type 為 u64, 但初始給值給 “Hello”
      • “Hello” 為字串型態,與初始宣告 type 不符合
  • 呼叫使用被初始化 struct 的值,使用.來進行呼叫
    • rectangle_1.width

fn 呼叫套用 struct


透過初始化 struct 之後, 可以利用 .來進行呼叫連接.

如果要把 struct 當成 參數[parameter]來帶入應用呢? 答案是可以的…

程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Rectangle {
width: u64,
height: u64
}

fn rectangle_area(rectangle: Rectangle) {
let area: u64 = rectangle.width * rectangle.height;
println!("This rectangle area is {}.",area)
}

fn main() {
// give a value by using initial struct.
let rectangle_1 = Rectangle {
width: 99,
height: 88
};
rectangle_area(rectangle_1); // Using function to caculate the area.
}

// output : This rectangle area is 8712.

Working Flow

  • 定義一個 function : fn rectangle_area(){}
  • 設定 parameter - parameter 名為 rectangle - parameter 型別為 Rectangle - fn rectangle_area(parameter_name: Struct_name){}
  • function 內部可直接使用 parameter_name.struct_name 來進行呼叫使用.
  • 連接一樣也是使用 .

[impl] implement 實作 – 具現化 實體操作


這是針對於 stuct 這個建構子 constructor 所制定專屬的方法.
想成實際的使用 constructor 所建立的專屬方法.
關鍵字就是 impl 英文單字: implement 實現

程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Create a struct, define the elements' name and type.
struct Rectangle {
width: u64,
height: u64
}

// Create a method to implement the struct, the method is belong struct.
impl Rectangle {
fn area(&self) -> u64{
return self.height * self.width
}
}

fn main() {
// give a value by using initial struct.
let rectangle_1 = Rectangle {
width: 99,
height: 88
};

println!("This rectangle width is {} cm, and the height is {} cm, and the are is {}", rectangle_1.width, rectangle_1.height, rectangle_1.area());
}

// output : This rectangle width is 99 cm, and the height is 88 cm, and the are is 8712

Working Flow

  • 定義一個 impl struct_name : 用來實現這個 struct 的方法.
  • impl Rectangle {定義方法}
  • 在 impl 內部, 可以有 function.
  • fn 的 parameter 必須要是 &self
  • &self 的意思 : 自己引用自己, 也就是可以使用 struct 裡面的結構子, 利用呼叫自己 (self) 連接(.) 結構子
  • 後面應用就可以直接應用 .area() 來使用此被 實現化出來的專屬方法.

完整程式碼


完整程式碼, 應用了兩種方式, 個別實現.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Create a struct, define the elements' name and type.
struct Rectangle {
width: u64,
height: u64
}

// Create a method to implement the struct, the method is belong struct.
impl Rectangle {
fn area(&self) -> u64{
return self.height * self.width
}
}

// Create a function to caculate the rectangle area, and print area out.
fn rectangle_area(rectangle: Rectangle) {
let area: u64 = rectangle.width * rectangle.height;
println!("This rectangle area is {}.",area)
}



fn main() {
// give a value by using initial struct.
let rectangle_1 = Rectangle {
width: 99,
height: 88
};

// using '.' to call value.
// rectangle_1.area() is from struct method "impl Rectangle"
println!("This rectangle width is {} cm, and the height is {} cm, and the are is {}", rectangle_1.width, rectangle_1.height, rectangle_1.area());
rectangle_area(rectangle_1); // Using function to caculate the area.
}

Rust_note_20211110