Rustlings

“Rustlings” 是一个由 Rust 社区维护的开源项目,旨在帮助初学者学习和练习 Rust 编程语言的基础知识。它提供了一系列的小练习。
github:https://github.com/rust-lang/rustlings
中文参考教材:https://www.rustwiki.org.cn/zh-CN/book/title-page.html

01_varables(ch3.1)

Ex 1

变量定义前加 let

1
2
3
4
 fn main() {
    let x = 5;
    println!("x has the value {}", x);
}

Ex 2

1
2
3
4
5
6
7
8
fn main() {
    let x = 10;
    if x == 10 {
        println!("x is ten!");
    } else {
        println!("x is not ten!");
    }
}

Ex 3

1
2
3
4
fn main() {
    let x: i32 = 0;
    println!("Number {}", x);
}

Ex 4

x 前加 mut ,代表 x 值可变

1
2
3
4
5
6
fn main() {
    let mut x = 3;
    println!("Number {}", x);
    x = 5; // don't change this line
    println!("Number {}", x);
}

Ex 5

1
2
3
4
5
6
fn main() {
    let number = "T-H-R-E-E"; // don't change this line
    println!("Spell a Number : {}", number);
    let number = 3; // don't rename this variable
    println!("Number plus two is : {}", number + 2);
}

Ex 6

1
2
3
4
const NUMBER: i32 = 3;
fn main() {
    println!("Number {}", NUMBER);
}

02_functions(ch3.3)

Ex 1

call_me()没有具体实现

1
2
3
4
5
6
7
fn main() {
    call_me();
}

fn call_me() {
    println!("Call me!");
}

Ex 2

call_me()参数没有具体类型

1
2
3
4
5
6
7
8
9
fn main() {
    call_me(3);
}

fn call_me(num: i32) {
    for i in 0..num {
        println!("Ring! Call number {}", i + 1);
    }
}

Ex 3

调用 call_me()时未传递具体参数

1
2
3
4
5
6
7
8
9
fn main() {
    call_me(3);
}

fn call_me(num: u32) {
    for i in 0..num {
        println!("Ring! Call number {}", i + 1);
    }
}

Ex 4

sale_price()没有添加返回值类型,在 -> 后加返回类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
fn main() {
    let original_price = 51;
    println!("Your sale price is {}", sale_price(original_price));
}

fn sale_price(price: i32) -> i32 {
    if is_even(price) {
        price - 10
    } else {
        price - 3
    }
}

fn is_even(num: i32) -> bool {
    num % 2 == 0
}

Ex 5

返回值不加分号,去掉 num * num 后的分号

1
2
3
4
5
6
7
8
fn main() {
    let answer = square(3);
    println!("The square of 3 is {}", answer);
}

fn square(num: i32) -> i32 {
    num * num
}

03_if(ch3.5)

Ex 1

一个简单的比大小

1
2
3
4
5
6
7
8
9
10
11
12
pub fn bigger(a: i32, b: i32) -> i32 {
    // Complete this function to return the bigger number!
    // If both numbers are equal, any of them can be returned.
    // Do not use:
    // - another function call
    // - additional variables
    if a > b {
        return a
    } else {
        b
    }
}

Ex 2

根据测试用例写出对应条件

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
pub fn foo_if_fizz(fizzish: &str) -> &str {
    if fizzish == "fizz" {
        "foo"
    } else if fizzish == "fuzz" {
        "bar"
    } else {
        "baz"
    }
}

// No test changes needed!
#[cfg(test)]
mod tests {
    use super::*;
   
    #[test]
    fn foo_for_fizz() {
        assert_eq!(foo_if_fizz("fizz"), "foo")
    }

    #[test]
    fn bar_for_fuzz() {
        assert_eq!(foo_if_fizz("fuzz"), "bar")
    }

    #[test]
    fn default_to_baz() {
        assert_eq!(foo_if_fizz("literally anything"), "baz")
    }
}

Ex 3

通过传入的字符串建立一个索引,然后通过索引返回对应的字符串

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pub fn animal_habitat(animal: &str) -> &'static str {
    let identifier = if animal == "crab" {
        1
    } else if animal == "gopher" {
        2
    } else if animal == "snake" {
        3
    } else {
        4
    };
    // DO NOT CHANGE THIS STATEMENT BELOW
    let habitat = if identifier == 1 {
        "Beach"
    } else if identifier == 2 {
        "Burrow"
    } else if identifier == 3 {
        "Desert"
    } else {
        "Unknown"
    };
    habitat
}

// No test changes needed.
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn gopher_lives_in_burrow() {
        assert_eq!(animal_habitat("gopher"), "Burrow")
    }

    #[test]
    fn snake_lives_in_desert() {
        assert_eq!(animal_habitat("snake"), "Desert")
    }

    #[test]
    fn crab_lives_on_beach() {
        assert_eq!(animal_habitat("crab"), "Beach")
    }

    #[test]
    fn unknown_animal() {
        assert_eq!(animal_habitat("dinosaur"), "Unknown")
    }
}

quiz1

很简单的一个题目

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
// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// Write a function that calculates the price of an order of apples given the
// quantity bought.
//
// No hints this time ;)

// I AM NOT DONE

// Put your function here!
fn calculate_price_of_apples(num: i32) -> i32 {
if num > 40 {
num
} else {
num * 2
}
}

// Don't modify this function!
#[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);
}