Skip to content

Commit

Permalink
feat: finish more rustlings
Browse files Browse the repository at this point in the history
  • Loading branch information
cncolder committed Jul 30, 2020
1 parent 7d696ec commit a88cd98
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 44 deletions.
4 changes: 2 additions & 2 deletions rustlings/exercises/primitive_types/primitive_types1.rs
Expand Up @@ -2,7 +2,7 @@
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)

// I AM NOT DONE
//

fn main() {
// Booleans (`bool`)
Expand All @@ -12,7 +12,7 @@ fn main() {
println!("Good morning!");
}

let // Finish the rest of this line like the example! Or make it be false!
let is_evening = true; // Finish the rest of this line like the example! Or make it be false!
if is_evening {
println!("Good evening!");
}
Expand Down
4 changes: 2 additions & 2 deletions rustlings/exercises/primitive_types/primitive_types2.rs
Expand Up @@ -2,7 +2,7 @@
// Fill in the rest of the line that has code missing!
// No hints, there's no tricks, just get used to typing these :)

// I AM NOT DONE
// 单引号字符, 双引号字符串.

fn main() {
// Characters (`char`)
Expand All @@ -16,7 +16,7 @@ fn main() {
println!("Neither alphabetic nor numeric!");
}

let // Finish this line like the example! What's your favorite character?
let your_character = '!'; // 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() {
Expand Down
5 changes: 3 additions & 2 deletions rustlings/exercises/primitive_types/primitive_types3.rs
Expand Up @@ -2,10 +2,11 @@
// Create an array with at least 100 elements in it where the ??? is.
// Execute `rustlings hint primitive_types3` for hints!

// I AM NOT DONE
// 什么时候 JS 能有这么方便 Range 类型.

fn main() {
let a = ???
// 1 到 100
let a = [1..100];

if a.len() >= 100 {
println!("Wow, that's a big array!");
Expand Down
5 changes: 3 additions & 2 deletions rustlings/exercises/primitive_types/primitive_types4.rs
Expand Up @@ -2,13 +2,14 @@
// Get a slice out of Array a where the ??? is so that the test passes.
// Execute `rustlings hint primitive_types4` for hints!!

// I AM NOT DONE
// 数组切片前要先借值.

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

let nice_slice = ???
// 从 1 到 3, 不包括 4.
let nice_slice = &a[1..4];

assert_eq!([2, 3, 4], nice_slice)
}
4 changes: 2 additions & 2 deletions rustlings/exercises/primitive_types/primitive_types5.rs
Expand Up @@ -2,11 +2,11 @@
// Destructure the `cat` tuple so that the println will work.
// Execute `rustlings hint primitive_types5` for hints!

// I AM NOT DONE
// Tuple 比数组语义更清晰.

fn main() {
let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat;
let (name, age) = cat;

println!("{} is {} years old.", name, age);
}
4 changes: 2 additions & 2 deletions rustlings/exercises/primitive_types/primitive_types6.rs
Expand Up @@ -3,9 +3,9 @@
// You can put this right into the `println!` where the ??? is.
// Execute `rustlings hint primitive_types6` for hints!

// I AM NOT DONE
// Tuple 的下标是属性

fn main() {
let numbers = (1, 2, 3);
println!("The second number is {}", ???);
println!("The second number is {}", numbers.1);
}
10 changes: 8 additions & 2 deletions rustlings/exercises/quiz1.rs
Expand Up @@ -7,10 +7,16 @@
// more than 40 at once, each apple only costs 1! Write a function that calculates
// the price of an order of apples given the order amount. No hints this time!

// I AM NOT DONE
// 来一箱苹果

// Put your function here!
// fn ..... {
fn calculate_apple_price(count: i32) -> i32 {
if (count <= 40) {
count * 2
} else {
count
}
}

// Don't modify this function!
#[test]
Expand Down
23 changes: 12 additions & 11 deletions rustlings/exercises/quiz2.rs
Expand Up @@ -7,7 +7,7 @@
// you think each value is. That is, add either `string_slice` or `string`
// before the parentheses on each line. If you're right, it will compile!

// I AM NOT DONE
// 要记住标准库方法的返回值类型才行.

fn string_slice(arg: &str) {
println!("{}", arg);
Expand All @@ -17,14 +17,15 @@ fn string(arg: String) {
}

fn main() {
("blue");
("red".to_string());
(String::from("hi"));
("rust is fun!".to_owned());
("nice weather".into());
(format!("Interpolation {}", "Station"));
(&String::from("abc")[0..1]);
(" hello there ".trim());
("Happy Monday!".to_string().replace("Mon", "Tues"));
("mY sHiFt KeY iS sTiCkY".to_lowercase());
string_slice("blue");
string("red".to_string());
string(String::from("hi"));
string("rust is fun!".to_owned());
string_slice("nice weather".into());
string("nice weather".into());
string(format!("Interpolation {}", "Station"));
string_slice(&String::from("abc")[0..1]);
string_slice(" hello there ".trim());
string("Happy Monday!".to_string().replace("Mon", "Tues"));
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
}
30 changes: 28 additions & 2 deletions rustlings/exercises/strings/strings1.rs
Expand Up @@ -2,13 +2,39 @@
// Make me compile without changing the function signature!
// Execute `rustlings hint strings1` for hints ;)

// I AM NOT DONE
// 多种写法

fn main() {
let answer = current_favorite_color();
println!("My current favorite color is {}", answer);
}

fn current_favorite_color() -> String {
"blue"
// 从 str 到 String 的多种写法

// 1. 初始化
// String::from("blue")

// 2. Display::to_string
// "blue".to_string()

// 3. std::borrow::ToOwned
// "blue".to_owned()

// 4. 根据类型自动判断, 此处是函数的返回值 String
// "blue".into()

// 5. 格式化
// format!("blue")

/*
* 就目前官方文档和社区讨论来看:
* 应该优先选择 1 和 2.
* 事实上 2 等于 1 等于 3.
* 4 取决于上下文
* 5 是调用 2 的宏.
* 综上所述, 考虑到简洁, 以及与其他类型转化成 String 的一致性, 首选 2.
* 如果值用于不同函数, 参数类型不定, 4 会很方便.
*/
"blue".to_string()
}
4 changes: 2 additions & 2 deletions rustlings/exercises/strings/strings2.rs
Expand Up @@ -2,11 +2,11 @@
// Make me compile without changing the function signature!
// Execute `rustlings hint strings2` for hints :)

// I AM NOT DONE
// 引用 String 会变成切片

fn main() {
let word = String::from("green"); // Try not changing this line :)
if is_a_color_word(word) {
if is_a_color_word(&word) {
println!("That is a color word I know!");
} else {
println!("That is not a color word I know.");
Expand Down
18 changes: 13 additions & 5 deletions rustlings/exercises/structs/structs1.rs
@@ -1,14 +1,19 @@
// structs1.rs
// Address all the TODOs to make the tests pass!

// I AM NOT DONE
// 结构拥有数据, 所以结构里不能使用 &str.

// 传统结构
struct ColorClassicStruct {
// TODO: Something goes here
name: String,
hex: String,
}

struct ColorTupleStruct(/* TODO: Something goes here */);
// 元组结构
struct ColorTupleStruct(/* TODO: Something goes here */ String, String);

// 单元结构
#[derive(Debug)]
struct UnitStruct;

Expand All @@ -19,7 +24,10 @@ mod tests {
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// let green =
let green = ColorClassicStruct {
name: "green".to_string(),
hex: "#00FF00".to_string(),
};

assert_eq!(green.name, "green");
assert_eq!(green.hex, "#00FF00");
Expand All @@ -28,7 +36,7 @@ mod tests {
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
// let green =
let green = ColorTupleStruct("green".to_string(), "#00FF00".to_string());

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

assert_eq!(message, "UnitStructs are fun!");
Expand Down
12 changes: 10 additions & 2 deletions rustlings/exercises/structs/structs2.rs
@@ -1,7 +1,7 @@
// structs2.rs
// Address all the TODOs to make the tests pass!

// I AM NOT DONE
// 复制结构时, 合并原结构的语法是两个点开头, 而且必须放在最后, 没有逗号结尾. 虽然放在最后, 但会跳过已经明确指定的字段.

#[derive(Debug)]
struct Order {
Expand All @@ -12,6 +12,8 @@ struct Order {
made_by_email: bool,
item_number: u32,
count: u32,


}

fn create_order_template() -> Order {
Expand All @@ -34,7 +36,13 @@ mod tests {
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// let your_order =
let your_order = Order {
name: "Hacker in Rust".to_string(),
count: 1,
..order_template
};


assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
Expand Down
20 changes: 12 additions & 8 deletions rustlings/exercises/structs/structs3.rs
Expand Up @@ -3,7 +3,7 @@
// exercise we have defined the Package struct and we want to test some logic attached to it,
// make the code compile and the tests pass! If you have issues execute `rustlings hint structs3`

// I AM NOT DONE
// 在结构基础上实现方法, 模仿类.

#[derive(Debug)]
struct Package {
Expand All @@ -16,17 +16,24 @@ impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
if weight_in_grams <= 0 {
// Something goes here...
panic!("Invalid weight")
} else {
return Package {sender_country, recipient_country, weight_in_grams};
return Package {
sender_country,
recipient_country,
weight_in_grams,
};
}
}

fn is_international(&self) -> ??? {
fn is_international(&self) -> bool {
// Something goes here...
self.sender_country != self.recipient_country
}

fn get_fees(&self, cents_per_kg: i32) -> ??? {
fn get_fees(&self, cents_per_kg: i32) -> i32 {
// Something goes here... (beware of grams to kg conversion)
self.weight_in_grams * cents_per_kg / 1000
}
}

Expand All @@ -47,7 +54,6 @@ mod tests {
fn create_international_package() {
let sender_country = String::from("Spain");
let recipient_country = String::from("Russia");

let package = Package::new(sender_country, recipient_country, 1200);

assert!(package.is_international());
Expand All @@ -58,10 +64,8 @@ mod tests {
let sender_country = String::from("Spain");
let recipient_country = String::from("Spain");

let cents_per_kg = ???;

let cents_per_kg = 3000;
let package = Package::new(sender_country, recipient_country, 1500);

assert_eq!(package.get_fees(cents_per_kg), 4500);
}
}

0 comments on commit a88cd98

Please sign in to comment.