Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exercises/00_intro/intro1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ fn main() {
println!("The file of this exercise is `exercises/00_intro/intro1.rs`. Have a look!");
println!("The current exercise path will be always shown under the progress bar.");
println!("You can click on the path to open the exercise file in your editor.");
println!("finish");
}
2 changes: 1 addition & 1 deletion exercises/00_intro/intro2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
// TODO: Fix the code to print "Hello world!".
printline!("Hello world!");
println!("Hello world!");
}
2 changes: 1 addition & 1 deletion exercises/01_variables/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Variables

In Rust, variables are immutable by default.
When a variable is immutable, once a value is bound to a name, you can't change that value.
When a variable is immutable, once a value is bound to a name, you cant change that value.
You can make them mutable by adding `mut` in front of the variable name.

## Further information
Expand Down
2 changes: 1 addition & 1 deletion exercises/01_variables/variables1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
// TODO: Add the missing keyword.
x = 5;
let x = 5;

println!("x has the value {x}");
}
4 changes: 2 additions & 2 deletions exercises/01_variables/variables2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fn main() {
// TODO: Change the line below to fix the compiler error.
let x;
let x = 10;

if x == 10 {
if 10 == x {
println!("x is ten!");
} else {
println!("x is not ten!");
Expand Down
2 changes: 1 addition & 1 deletion exercises/01_variables/variables3.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
// TODO: Change the line below to fix the compiler error.
let x: i32;
let x: i32 = 40;

println!("Number {x}");
}
7 changes: 3 additions & 4 deletions exercises/01_variables/variables4.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// TODO: Fix the compiler error.
fn main() {
let x = 3;
let mut x = 3; // 加上 mut 表示 x 是可变的
println!("Number {x}");

x = 5; // Don't change this line
x = 5; // 现在可以修改了
println!("Number {x}");
}
}
6 changes: 3 additions & 3 deletions exercises/01_variables/variables5.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fn main() {
let number = "T-H-R-E-E"; // Don't change this line
println!("Spell a number: {number}");
println!("Spell a number: {}", number);

// TODO: Fix the compiler error by changing the line below without renaming the variable.
number = 3;
let number = 3; // 使用变量遮蔽,重新绑定为整数
println!("Number plus two is: {}", number + 2);
}
}
2 changes: 1 addition & 1 deletion exercises/01_variables/variables6.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: Change the line below to fix the compiler error.
const NUMBER = 3;
const NUMBER: i32 = 3;

fn main() {
println!("Number: {NUMBER}");
Expand Down
3 changes: 3 additions & 0 deletions exercises/02_functions/functions1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// TODO: Add some function with the name `call_me` without arguments or a return value.

fn call_me() {
// 函数体可以为空
}
fn main() {
call_me(); // Don't change this line
}
2 changes: 1 addition & 1 deletion exercises/02_functions/functions2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: Add the missing type of the argument `num` after the colon `:`.
fn call_me(num:) {
fn call_me(num:usize) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_functions/functions3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ fn call_me(num: u8) {

fn main() {
// TODO: Fix the function call.
call_me();
call_me(2);
}
2 changes: 1 addition & 1 deletion exercises/02_functions/functions4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn is_even(num: i64) -> bool {
}

// TODO: Fix the function signature.
fn sale_price(price: i64) -> {
fn sale_price(price: i64) -> i64{
if is_even(price) {
price - 10
} else {
Expand Down
2 changes: 1 addition & 1 deletion exercises/02_functions/functions5.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO: Fix the function body without changing the signature.
fn square(num: i32) -> i32 {
num * num;
num * num
}

fn main() {
Expand Down
5 changes: 5 additions & 0 deletions exercises/03_if/if1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ fn bigger(a: i32, b: i32) -> i32 {
// Do not use:
// - another function call
// - additional variables
if a > b {
a
} else {
b
}
}

fn main() {
Expand Down
8 changes: 5 additions & 3 deletions exercises/03_if/if2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
fn picky_eater(food: &str) -> &str {
if food == "strawberry" {
"Yummy!"
} else {
1
}else if food == "potato" {
"I guess I can eat that."
}else {
"No thanks!"
}
}

Expand All @@ -19,7 +21,7 @@ mod tests {

#[test]
fn yummy_food() {
// This means that calling `picky_eater` with the argument "strawberry" should return "Yummy!".
// This means that calling `picky_eater` with the argument "food" should return "Yummy!".
assert_eq!(picky_eater("strawberry"), "Yummy!");
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/03_if/if3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ fn animal_habitat(animal: &str) -> &str {
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
2.0
2
} else if animal == "snake" {
3
} else {
"Unknown"
0
};

// Don't change the expression below!
Expand Down
1 change: 1 addition & 0 deletions exercises/04_primitive_types/primitive_types1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
// TODO: Define a boolean variable with the name `is_evening` before the `if` statement below.
// The value of the variable should be the negation (opposite) of `is_morning`.
// let …
let is_evening = !is_morning;
if is_evening {
println!("Good evening!");
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
// Try a letter, try a digit (in single quotes), try a special character, try a character
// from a different language than your own, try an emoji 😉
// let your_character = '';

let your_character = '😉'; // 这里可以替换成你喜欢的任意字符
if your_character.is_alphabetic() {
println!("Alphabetical!");
} else if your_character.is_numeric() {
Expand Down
3 changes: 2 additions & 1 deletion exercises/04_primitive_types/primitive_types3.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
fn main() {
// TODO: Create an array called `a` with at least 100 elements in it.
// let a = ???

let a = [0; 100]; // 100个元素,每个元素都是0

if a.len() >= 100 {
println!("Wow, that's a big array!");
} else {
Expand Down
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
#[test]
fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5];

let nice_slice = &a[1..4];
// TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
// let nice_slice = ???

Expand Down
1 change: 1 addition & 0 deletions exercises/04_primitive_types/primitive_types5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ fn main() {

// TODO: Destructure the `cat` tuple in one statement so that the println works.
// let /* your pattern here */ = cat;
let (name, age) = cat;

println!("{name} is {age} years old");
}
2 changes: 1 addition & 1 deletion exercises/04_primitive_types/primitive_types6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod tests {
#[test]
fn indexing_tuple() {
let numbers = (1, 2, 3);

let second = numbers.1;
// TODO: Use a tuple index to access the second element of `numbers`
// and assign it to a variable called `second`.
// let second = ???;
Expand Down
4 changes: 2 additions & 2 deletions exercises/05_vecs/vecs1.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // Array

// TODO: Create a vector called `v` which contains the exact same elements as in the array `a`.
// Use the vector macro.
// let v = ???;

let v = vec![10, 20, 30, 40];
(a, v)
}

Expand Down
8 changes: 2 additions & 6 deletions exercises/05_vecs/vecs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fn vec_loop(input: &[i32]) -> Vec<i32> {
for element in input {
// TODO: Multiply each element in the `input` slice by 2 and push it to
// the `output` vector.
output.push(element * 2);
}

output
Expand All @@ -21,12 +22,7 @@ fn vec_map(input: &[i32]) -> Vec<i32> {
// by 2, but with iterator mapping instead of manually pushing into an empty
// vector.
// See the example in the function `vec_map_example` above.
input
.iter()
.map(|element| {
// ???
})
.collect()
input.iter().map(|element| element * 2).collect()
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions exercises/06_move_semantics/move_semantics1.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO: Fix the compiler error in this function.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
let vec = vec;
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
let mut vec = vec;

vec.push(88);

Expand Down
2 changes: 1 addition & 1 deletion exercises/06_move_semantics/move_semantics2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod tests {
fn move_semantics2() {
let vec0 = vec![22, 44, 66];

let vec1 = fill_vec(vec0);
let vec1 = fill_vec(vec0.clone());

assert_eq!(vec0, [22, 44, 66]);
assert_eq!(vec1, [22, 44, 66, 88]);
Expand Down
2 changes: 1 addition & 1 deletion exercises/06_move_semantics/move_semantics3.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO: Fix the compiler error in the function without adding any new line.
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
fn fill_vec(mut vec: Vec<i32>) -> Vec<i32> {
vec.push(88);

vec
Expand Down
2 changes: 1 addition & 1 deletion exercises/06_move_semantics/move_semantics4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod tests {
fn move_semantics4() {
let mut x = Vec::new();
let y = &mut x;
let z = &mut x;
y.push(42);
let z = &mut x;
z.push(13);
assert_eq!(x, [42, 13]);
}
Expand Down
10 changes: 5 additions & 5 deletions exercises/06_move_semantics/move_semantics5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
// removing references (the character `&`).

// Shouldn't take ownership
fn get_char(data: String) -> char {
fn get_char(data: &String) -> char {
data.chars().last().unwrap()
}

// Should take ownership
fn string_uppercase(mut data: &String) {
data = data.to_uppercase();
fn string_uppercase(data: String) {
let data = data.to_uppercase();

println!("{data}");
}

fn main() {
let data = "Rust is great!".to_string();

get_char(data);
get_char(&data);

string_uppercase(&data);
string_uppercase(data.clone());
}
8 changes: 7 additions & 1 deletion exercises/07_structs/structs1.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
struct ColorRegularStruct {
// TODO: Add the fields that the test `regular_structs` expects.
// What types should the fields have? What are the minimum and maximum values for RGB colors?
red: u8,
green: u8,
blue: u8,
}

struct ColorTupleStruct(/* TODO: Add the fields that the test `tuple_structs` expects */);
struct ColorTupleStruct(u8, u8, u8);

#[derive(Debug)]
struct UnitStruct;
Expand All @@ -20,6 +23,7 @@ mod tests {
fn regular_structs() {
// TODO: Instantiate a regular struct.
// let green =
let green = ColorRegularStruct { red: 0, green: 255, blue: 0 };

assert_eq!(green.red, 0);
assert_eq!(green.green, 255);
Expand All @@ -30,6 +34,7 @@ mod tests {
fn tuple_structs() {
// TODO: Instantiate a tuple struct.
// let green =
let green = ColorTupleStruct(0, 255, 0);

assert_eq!(green.0, 0);
assert_eq!(green.1, 255);
Expand All @@ -40,6 +45,7 @@ mod tests {
fn unit_structs() {
// TODO: Instantiate a unit struct.
// let unit_struct =
let unit_struct = UnitStruct;
let message = format!("{unit_struct:?}s are fun!");

assert_eq!(message, "UnitStructs are fun!");
Expand Down
1 change: 1 addition & 0 deletions exercises/07_structs/structs2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod tests {

// TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = Order {name: String::from("Hacker in Rust"), count: 1, ..order_template};

assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
Expand Down
6 changes: 4 additions & 2 deletions exercises/07_structs/structs3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ impl Package {
}

// TODO: Add the correct return type to the function signature.
fn is_international(&self) {
fn is_international(&self) -> bool {
// TODO: Read the tests that use this method to find out when a package
// is considered international.
self.sender_country != self.recipient_country
}

// TODO: Add the correct return type to the function signature.
fn get_fees(&self, cents_per_gram: u32) {
fn get_fees(&self, cents_per_gram: u32) -> u32{
// TODO: Calculate the package's fees.
self.weight_in_grams * cents_per_gram
}
}

Expand Down
4 changes: 2 additions & 2 deletions exercises/08_enums/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Enums

Rust allows you to define types called "enums" which enumerate possible values.
Enums are a feature in many languages, but their capabilities differ in each language. Rust's enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.
Enums are a feature in many languages, but their capabilities differ in each language. Rusts enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell.
Useful in combination with enums is Rust's "pattern matching" facility, which makes it easy to run different code for different values of an enumeration.

## Further information

- [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html)
- [Pattern syntax](https://doc.rust-lang.org/book/ch19-03-pattern-syntax.html)
- [Pattern syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html)
Loading