iT邦幫忙

2023 iThome 鐵人賽

DAY 12
0
自我挑戰組

Let's go Rusty. 從0開始了解Rust.系列 第 12

Day 12 - 派生屬性(Derive) & 參數和引數的差異(Difference between parameter and argument)

  • 分享至 

  • xImage
  •  

有英文版文章
Also this tutorial has been written in English
Check out my Medium

Rust的中文翻譯以參考這份檔案為主.


目錄

  • 派生屬性(Derive)
  • 參數和引數的差異(Difference between parameter and argument)

派生屬性(Derive)

  • 比較性質(traits): Eq, PartialEq, Ord, PartialOrd.
  • Clone, 從&T 建立 &T 通過copy(見下一項)
  • Copy, 給予一種型別"copy語意(copy semantics),而非移動語意(move semantics)
  • Hash, 從&T運算雜湊(compute a hash from &T)
  • Default, 創建一個檔案型別的空實例(to create an empty instance of a data type)
  • Debug, 安排使用{:?}格式的值(to format a value using the {:?} formatter)

Example 01 - 沒有派生屬性 Without Derive trait(s).

enum Title {
  CEO,
  CTO,
  Engineer
}
struct Employee {
  position: Title,
  work_hour: i64
}

fn main() {
    let person_a = Employee{
      position: Title::CTO,
      work_hour:25
    };
  match person_a.position {
    Title::CEO => println!("CEO"),
    Title::CTO => println!("CTO"),
    Title::Engineer => println!("Engineer"),
  }
}

/*
CTO
*/

Example 02 - Debug派生屬性

#[derive(Debug)]
enum Title {
    CEO,
    CTO,
    Engineer,
}

struct Employee {
    position: Title,
    work_hour: i64,
}

fn main() {
    let person_a = Employee {
        position: Title::CTO,
        work_hour: 25,
    };
    println!("{:?}", person_a.position);
}

/*
CTO
*/

Example 03

#[derive(Debug)]
enum Title {
    CEO,
    CTO,
    Engineer,
}

#[derive(Debug)]
struct Employee {
    position: Title,
    work_hour: i64,
}

fn main() {
    let person_a = Employee {
        position: Title::CTO,
        work_hour: 25,
    };
    println!("{:?}", person_a);
}

/*
Employee { position: CTO, work_hour: 25 }
*/

Example 04 - #[derive(Clone, Copy)]

#[derive(Debug, Clone, Copy)]
enum Title {
    CEO,
    CTO,
    Engineer,
}

#[derive(Debug, Clone, Copy)]
struct Employee {
    position: Title,
    work_hour: i64,
}

fn print_employee(emp: Employee) {
    println!("{:?}", emp);
}

fn main() {
    let person_a = Employee {
        position: Title::CTO,
        work_hour: 25,
    };
    let person_b = Employee {
        position: Title::Engineer,
        work_hour: 40,
    };
    print_employee(person_a);
    print_employee(person_b);
}

/*
Employee { position: CTO, work_hour: 25 }
Employee { position: Engineer, work_hour: 40 }
*/



參數和引數的差異(Difference between parameter and argument)

參數和引數的差異(Difference between parameter and argument)

  • 引數 (Argument) 是用於呼叫函式,
  • 參數 (Parameter) 是方法簽章 (方法的宣告)。

解釋Explanation

什麼是過程式程式Procedural programming

    • 過程定義了一個參數 (Parameter),呼叫的程式碼會傳送一個引數到那個參數(The procedure defines a parameter, and the calling code passes an argument to that parameter)
  • 一個引數表示當你呼叫一個procedure,並傳送到過程參數的值。當被呼叫的程式碼呼叫了procedure,會供應參數。An argument represents the value that you pass to a procedure parameter when you call the procedure. The calling code supplies the arguments when it calls the procedure.

void Foo(int i, float f)
{
    // Do things
}

void Bar()
{
    int num = 1;
    Foo(anInt, 2.0);
}
  • num 與 2.0 即為 引數 (Argument),或稱為 函式引數 (Argument of a Function)。
  • i 與 f 則為 參數 (Parameter)。
    例子引用自https://notfalse.net/6/arg-vs-param

參考資料 Reference

Nice 教學影片


上一篇
Day 11 - 字串(Strings)
下一篇
Day 13 - 型別標記(Type Annotation) & 進階Match(Advanced Match)
系列文
Let's go Rusty. 從0開始了解Rust.15
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言