04_primitive_types(ch3.2, ch4.3)

Ex 1

so eazy,给is_evening随便加个bool值就行

1
2
3
4
5
6
7
8
9
10
11
12
13
fn main() {
// Booleans (`bool`)

let is_morning = true;
if is_morning {
println!("Good morning!");
}

let is_evening = false;// Finish the rest of this line like the example! Or make it be false!
if is_evening {
println!("Good evening!");
}
}

Ex 2

这个题目中,is_alphabetic()和is_numeric()是字符(char)类型的方法,用于检查字符是否为字母或数字。这些方法属于标准库的一部分
练习还是很简单,加个字符就行

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
fn main() {
// Characters (`char`)

// Note the _single_ quotes, these are different from the double quotes
// you've been seeing around.
let my_first_initial = 'C';
if my_first_initial.is_alphabetic() {
println!("Alphabetical!");
} else if my_first_initial.is_numeric() {
println!("Numerical!");
} else {
println!("Neither alphabetic nor numeric!");
}

let your_character = 'B';// Finish this line like the example! What's your favorite character?
// Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji!
if your_character.is_alphabetic() {
println!("Alphabetical!");
} else if your_character.is_numeric() {
println!("Numerical!");
} else {
println!("Neither alphabetic nor numeric!");
}
}

Ex 3

这里要求实现一个长度大于100的数组
我们在方括号内以逗号分隔的列表形式将值写到数组中:

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

当希望将数据分配到栈(stack)而不是堆(heap)时,或者希望确保始终具有固定数量的元素时,数组特别有用。但它们不像 vector(动态数组,可变数组)类型那么灵活。vector类型类似于标准库中提供的集合类型,其大小允许增长或缩小。如果不确定是使用数组还是 vector,那就应该使用一个vector。
不过当你明确元素数量不需要改变时,数组会更有用。例如,如果你在程序中使用月份的名称,你很可能希望使用的是数组而不是vector,因为你知道它始终包含12个元素:

1
2
let months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];

使用方括号编写数组的类型,其中包含每个元素的类型、分号,然后是数组中的元素数,如下所示:

1
let a: [i32; 5] = [1, 2, 3, 4, 5];

这里,i32 是每个元素的类型。分号之后,数字 5 表明该数组包含 5 个元素。
对于每个元素都相同的情况,还可以通过指定初始值、后跟分号和方括号中的数组长度来初始化数组

1
let a = [3; 5];

变量名为 a 的数组将包含 5 个元素,这些元素的值初始化为 3。这种写法与 let a = [3, 3, 3, 3, 3]; 效果相同,但更简洁。
这道题就很简单了

1
2
3
4
5
6
7
8
9
10
fn main() {
let a = [1; 100];

if a.len() >= 100 {
println!("Wow, that's a big array!");
} else {
println!("Meh, I eat arrays like that for breakfast.");
panic!("Array not big enough, more elements needed")
}
}

Ex 4

这里要求我们对数组进行切片(slice)操作,切片在ownership中会详细讲到,这里我们只需要知道对数组a索引为m到n的切片用

1
let slice = &a[m..n+1];

表示
那么这题就很容易写了

1
2
3
4
5
6
7
8
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];

let nice_slice = &a[1..4];

assert_eq!([2, 3, 4], nice_slice)
}

Ex 5

考察元组
元组是将多种类型的多个值组合到一个复合类型中的一种基本方式。元组的长度是固定的:声明后,它们就无法增长或缩小。
我们通过在小括号内写入以逗号分隔的值列表来创建一个元组。元组中的每个位置都有一个类型,并且元组中不同值的类型不要求是相同的。我们在下面示例中添加了可选的类型标注:

1
2
3
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
}

变量 tup 绑定到整个元组,因为元组被认作是单个复合元素。 想从元组中获取个别值,我们可以使用模式匹配来解构(destructure)元组的一个值,如下所示:

1
2
3
4
5
6
7
fn main() {
let tup = (500, 6.4, 1);

let (x, y, z) = tup;

println!("The value of y is: {}", y);
}

该程序首先创建一个元组并将其绑定到变量 tup 上。 然后它借助 let 来使用一个模式匹配 tup,并将它分解成三个单独的变量 x、y 和 z。 这过程称为解构(destructuring),因为它将单个元组分为三部分。最后,程序打印出 y 值,为 6.4。
除了通过模式匹配进行解构外,我们还可以使用一个句点(.)连上要访问的值的索引来直接访问元组元素。例如:

1
2
3
4
5
6
7
8
9
fn main() {
let x: (i32, f64, u8) = (500, 6.4, 1);

let five_hundred = x.0;

let six_point_four = x.1;

let one = x.2;
}

该程序创建一个元组 x,然后通过使用它们的索引为每个元素创建新的变量。和大多数编程语言一样,元组中的第一个索引为 0。
没有任何值的元组 () 是一种特殊的类型,只有一个值,也写成 ()。该类型被称为单元类型(unit type),该值被称为单元值(unit value)。如果表达式不返回任何其他值,就隐式地返回单元值

回顾看这道题,直接对其解构就行

1
2
3
4
5
6
fn main() {
let cat = ("Furry McFurson", 3.5);
let (name, age)= cat;

println!("{} is {} years old.", name, age);
}

Ex 6

即前面说的使用句点访问元组元素

1
2
3
4
5
6
7
8
9
#[test]
fn indexing_tuple() {
let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax.
let second = numbers.1;

assert_eq!(2, second,
"This is not the 2nd number in the tuple!")
}

05_vecs(&8.1)

Ex 1

建立一个和数组a一样的vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array
let v = vec![10, 20, 30, 40];// TODO: declare your vector here with the macro for vectors

(a, v)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_array_and_vec_similarity() {
let (a, v) = array_and_vec();
assert_eq!(a, v[..]);
}
}

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
31
32
33
34
35
36
37
38
39
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for element in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2.
*element *= 2;
}

// At this point, `v` should be equal to [4, 8, 12, 16, 20].
v
}

fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|element| {
// TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number!
element * 2
}).collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_vec_loop() {
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
let ans = vec_loop(v.clone());

assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
}

#[test]
fn test_vec_map() {
let v: Vec<i32> = (1..).filter(|x| x % 2 == 0).take(5).collect();
let ans = vec_map(&v);

assert_eq!(ans, v.iter().map(|x| x * 2).collect::<Vec<i32>>());
}
}