你沒看錯,Rustlings 連隨堂考都有。今天原本要學習第 4 單元,結果發現 Rustlings 要我先來考試。

上一篇:03_if
本系列主頁面:Rust繁中簡學!
繁體中文版 Rustlings:https://github.com/TimLai666/rustlings-zh-TW
安裝方法:00_intro

quiz1.rs(第 1 ~ 3 單元複習考)

// quiz1.rs
//
// 本測驗涵蓋以下章節:
// - 變數
// - 函數
// - If
//
// Mary 在買蘋果。蘋果的價格計算如下:
// - 一個蘋果的價格是 2 rustbucks。
// - 如果 Mary 買超過 40 個蘋果,每個蘋果的價格只需 1 rustbuck!
// 寫一個函數來計算購買蘋果的總價格,給定購買的數量。
//
// 這次沒有提示 ;)

// I AM NOT DONE

// 把你的函數寫在這裡!
// fn calculate_price_of_apples {

// 不要修改這個函數!
#[test]
fn verify_test() {
    let price1 = calculate_price_of_apples(35);
    let price2 = calculate_price_of_apples(40);
    let price3 = calculate_price_of_apples(41);
    let price4 = calculate_price_of_apples(65);

    assert_eq!(70, price1);
    assert_eq!(80, price2);
    assert_eq!(41, price3);
    assert_eq!(65, price4);
}
! 編譯 exercises/quiz1.rs 失敗!請再試一次。以下是輸出:
error[E0425]: cannot find function `calculate_price_of_apples` in this scope
  --> exercises/quiz1.rs:23:18
   |
23 |     let price1 = calculate_price_of_apples(35);
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `calculate_price_of_apples` in this scope
  --> exercises/quiz1.rs:24:18
   |
24 |     let price2 = calculate_price_of_apples(40);
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `calculate_price_of_apples` in this scope
  --> exercises/quiz1.rs:25:18
   |
25 |     let price3 = calculate_price_of_apples(41);
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope

error[E0425]: cannot find function `calculate_price_of_apples` in this scope
  --> exercises/quiz1.rs:26:18
   |
26 |     let price4 = calculate_price_of_apples(65);
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0425`.

這個測驗將會考驗我們對變數、函數和 if 的熟悉度。題目如下:

Mary 在買蘋果。蘋果的價格計算如下:

  • 一個蘋果的價格是 2 rustbucks。
  • 如果 Mary 買超過 40 個蘋果,每個蘋果的價格只需 1 rustbuck!

寫一個函數來計算購買蘋果的總價格,給定購買的數量。

看起來不難,是個計算蘋果價格的題目。(rustbucks 是虛構的貨幣)

我們把下面這行取消註解,開始寫函數。VS Code 只要按 Ctrl + / 即可對游標所在的那行快速取消或加上註解。

// 把你的函數寫在這裡!
// fn calculate_price_of_apples {

我們觀察一下程式是怎麼呼叫這個函數的:

    let price1 = calculate_price_of_apples(35);
    let price2 = calculate_price_of_apples(40);
    let price3 = calculate_price_of_apples(41);
    let price4 = calculate_price_of_apples(65);

看來它會傳入一個整數(蘋果的數量)到這個函數,然後函數會回傳算好的價格。

fn calculate_price_of_apples(quantity: u32) -> u32 {

定義一個叫 quantity 的參數來接收蘋果的數量,這裡我們用 u32,因為數量不可能是負數。
由於這個函數會回傳值(價格),並且這個值應該也會是 u32 的資料型態,所以在小括號後面加上箭頭寫 u32

接著來做函數裡的判斷邏輯。

fn calculate_price_of_apples(quantity: u32) -> u32 {
    if quantity > 40 {
        1 * quantity
    } else {
        2 * quantity
    }
}

依據題意,如果蘋果數量超過 40 顆,每顆就只要 1 元,否則就是每顆 2 元。因此我們用一個 if 來判斷蘋果數量有沒有大於 40,如果有,就回傳 1 乘以數量;如果沒有,就回傳 2 乘以數量。

✓ 成功測試 exercises/quiz1.rs!

🎉 🎉 代碼正在編譯,並且測試通過! 🎉 🎉

您可以繼續進行此練習,
或通過刪除 `I AM NOT DONE` 註釋來進入下一個練習:

13 |  // 這次沒有提示 ;)
14 |  
15 |  // I AM NOT DONE
16 |  
17 |  // 把你的函數寫在這裡!

測驗通過!

如果你覺得直接回傳 1 * quantity 和 2 * quantity 會不容易理解,可以宣告一個變數來存價格,判斷完後再用價格去乘以數量。

fn calculate_price_of_apples(quantity: u32) -> u32 {
    let unit_price: u32;
    if quantity > 40 {
        unit_price = 1;
    } else {
        unit_price = 2;
    }
    
    unit_price * quantity
}
✓ 成功測試 exercises/quiz1.rs!

🎉 🎉 代碼正在編譯,並且測試通過! 🎉 🎉

您可以繼續進行此練習,
或通過刪除 `I AM NOT DONE` 註釋來進入下一個練習:

13 |  // 這次沒有提示 ;)
14 |  
15 |  // I AM NOT DONE
16 |  
17 |  // 把你的函數寫在這裡!

一樣是編譯成功。

關於 Rust 函數,我們要記住的一點是,它會自動回傳最後一行表達式,並且如果我們希望它回傳,就不能加分號
如果擔心記不住,也可以腳踏實地的用 return 來回傳:

fn calculate_price_of_apples(quantity: u32) -> u32 {
    let unit_price: u32;
    if quantity > 40 {
        unit_price = 1;
    } else {
        unit_price = 2;
    }

    return unit_price * quantity;
}

Similar Posts

One Comment

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *