This commit is contained in:
Cody 2023-09-27 14:03:17 +08:00
parent 9a743f80c5
commit 049ab5e619
No known key found for this signature in database
GPG Key ID: C7228F849611151F
25 changed files with 100 additions and 50 deletions

View File

@ -3,8 +3,12 @@
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a // Execute `rustlings hint functions1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
call_me(); call_me();
} }
fn call_me(){
}

View File

@ -3,13 +3,13 @@
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a // Execute `rustlings hint functions2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
call_me(3); call_me(3);
} }
fn call_me(num:) { fn call_me(num: i32) {
for i in 0..num { for i in 0..num {
println!("Ring! Call number {}", i + 1); println!("Ring! Call number {}", i + 1);
} }

View File

@ -3,10 +3,10 @@
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a // Execute `rustlings hint functions3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
call_me(); call_me(100);
} }
fn call_me(num: u32) { fn call_me(num: u32) {

View File

@ -8,14 +8,14 @@
// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a // Execute `rustlings hint functions4` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let original_price = 51; let original_price = 51;
println!("Your sale price is {}", sale_price(original_price)); println!("Your sale price is {}", sale_price(original_price));
} }
fn sale_price(price: i32) -> { fn sale_price(price: i32) -> i32 {
if is_even(price) { if is_even(price) {
price - 10 price - 10
} else { } else {

View File

@ -3,7 +3,7 @@
// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a // Execute `rustlings hint functions5` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let answer = square(3); let answer = square(3);
@ -11,5 +11,6 @@ fn main() {
} }
fn square(num: i32) -> i32 { fn square(num: i32) -> i32 {
num * num; // num * num;
num * num
} }

View File

@ -2,13 +2,18 @@
// //
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE // I AM DONE
pub fn bigger(a: i32, b: i32) -> i32 { pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number! // Complete this function to return the bigger number!
// Do not use: // Do not use:
// - another function call // - another function call
// - additional variables // - additional variables
if ( a >= b ){
a
}else{
b
}
} }
// Don't mind this for now :) // Don't mind this for now :)

View File

@ -5,13 +5,17 @@
// //
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE // I AM DONE
pub fn foo_if_fizz(fizzish: &str) -> &str { pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" { if fizzish == "fizz" {
"foo" "foo"
// } else if (fizzish == "fuzz") {
// "bar"
} else if fizzish == "fuzz" {
"bar"
} else { } else {
1 "baz"
} }
} }

View File

@ -2,17 +2,18 @@
// //
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE // I AM DONE
pub fn animal_habitat(animal: &str) -> &'static str { pub fn animal_habitat(animal: &str) -> &'static str {
let identifier = if animal == "crab" { let identifier = if animal == "crab" {
1 1
} else if animal == "gopher" { } else if animal == "gopher" {
2.0 // 2.0
2
} else if animal == "snake" { } else if animal == "snake" {
3 3
} else { } else {
"Unknown" 4
}; };
// DO NOT CHANGE THIS STATEMENT BELOW // DO NOT CHANGE THIS STATEMENT BELOW

View File

@ -13,7 +13,7 @@
// Execute `rustlings hint intro1` or use the `hint` watch subcommand for a // Execute `rustlings hint intro1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
println!("Hello and"); println!("Hello and");

View File

@ -5,8 +5,9 @@
// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a // Execute `rustlings hint intro2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
println!("Hello {}!"); let string: String = "World".to_string();
println!("Hello {}!", string);
} }

View File

@ -3,7 +3,7 @@
// Fill in the rest of the line that has code missing! No hints, there's no // Fill in the rest of the line that has code missing! No hints, there's no
// tricks, just get used to typing these :) // tricks, just get used to typing these :)
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
// Booleans (`bool`) // Booleans (`bool`)
@ -13,7 +13,8 @@ fn main() {
println!("Good morning!"); 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!
// let is_evening = false;
if is_evening { if is_evening {
println!("Good evening!"); println!("Good evening!");
} }

View File

@ -3,7 +3,7 @@
// Fill in the rest of the line that has code missing! No hints, there's no // Fill in the rest of the line that has code missing! No hints, there's no
// tricks, just get used to typing these :) // tricks, just get used to typing these :)
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
// Characters (`char`) // Characters (`char`)
@ -19,7 +19,9 @@ fn main() {
println!("Neither alphabetic nor numeric!"); println!("Neither alphabetic nor numeric!");
} }
let // Finish this line like the example! What's your favorite character? let your_character = 'A';// Finish this line like the example! What's your favorite character?
let your_character = '😘';
let your_character = '你';
// Try a letter, try a number, try a special character, try a character // Try a letter, try a number, try a special character, try a character
// from a different language than your own, try an emoji! // from a different language than your own, try an emoji!
if your_character.is_alphabetic() { if your_character.is_alphabetic() {

View File

@ -5,10 +5,10 @@
// Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand // Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let a = ??? let a = ["nihao";100];
if a.len() >= 100 { if a.len() >= 100 {
println!("Wow, that's a big array!"); println!("Wow, that's a big array!");
@ -16,4 +16,6 @@ fn main() {
println!("Meh, I eat arrays like that for breakfast."); println!("Meh, I eat arrays like that for breakfast.");
panic!("Array not big enough, more elements needed") panic!("Array not big enough, more elements needed")
} }
// println!("{:?}", a);
} }

View File

@ -5,13 +5,14 @@
// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand // Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE // I AM DONE
#[test] #[test]
fn slice_out_of_array() { fn slice_out_of_array() {
let a = [1, 2, 3, 4, 5]; let a = [1, 2, 3, 4, 5];
let nice_slice = ??? // let nice_slice = &a[1..3];
let nice_slice = &a[1..4];
assert_eq!([2, 3, 4], nice_slice) assert_eq!([2, 3, 4], nice_slice)
} }

View File

@ -5,11 +5,13 @@
// Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand // Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let cat = ("Furry McFurson", 3.5); let cat = ("Furry McFurson", 3.5);
let /* your pattern here */ = cat; // let /* your pattern here */ = cat;
let (name, age) = cat;
let (name, age): (&str, f32) = cat;
println!("{} is {} years old.", name, age); println!("{} is {} years old.", name, age);
} }

View File

@ -3,16 +3,17 @@
// Use a tuple index to access the second element of `numbers`. You can put the // Use a tuple index to access the second element of `numbers`. You can put the
// expression for the second element where ??? is so that the test passes. // expression for the second element where ??? is so that the test passes.
// //
// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand // Execute ` ` or use the `hint` watch subcommand
// for a hint. // for a hint.
// I AM NOT DONE // I AM DONE
#[test] #[test]
fn indexing_tuple() { fn indexing_tuple() {
let numbers = (1, 2, 3); let numbers = (1, 2, 3);
// Replace below ??? with the tuple indexing syntax. // Replace below ??? with the tuple indexing syntax.
let second = ???; // 用index取元组中的值
let second = numbers.1;
assert_eq!(2, second, assert_eq!(2, second,
"This is not the 2nd number in the tuple!") "This is not the 2nd number in the tuple!")

View File

@ -13,10 +13,19 @@
// //
// No hints this time ;) // No hints this time ;)
// I AM NOT DONE // I AM DONE
// Put your function here! // Put your function here!
// fn calculate_price_of_apples { fn calculate_price_of_apples(count: i32) -> i32{
let price: i32;
if count <= 40 && count > 0{
count * 2
} else if count > 40 {
count * 1
} else {
0
}
}
// Don't modify this function! // Don't modify this function!
#[test] #[test]

View File

@ -5,9 +5,9 @@
// Execute `rustlings hint variables1` or use the `hint` watch subcommand for a // Execute `rustlings hint variables1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
x = 5; let x = 5;
println!("x has the value {}", x); println!("x has the value {}", x);
} }

View File

@ -3,10 +3,13 @@
// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a // Execute `rustlings hint variables2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let x; // let x: i32 = 0;
// 类型推断
// Type inference
let x = 0;
if x == 10 { if x == 10 {
println!("x is ten!"); println!("x is ten!");
} else { } else {

View File

@ -3,9 +3,9 @@
// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a // Execute `rustlings hint variables3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let x: i32; let x: i32 = 10;
println!("Number {}", x); println!("Number {}", x);
} }

View File

@ -3,10 +3,12 @@
// Execute `rustlings hint variables4` or use the `hint` watch subcommand for a // Execute `rustlings hint variables4` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let x = 3; // Variable bindings are immutable by default
// 变量绑定默认是不可变的
let mut x = 3;
println!("Number {}", x); println!("Number {}", x);
x = 5; // don't change this line x = 5; // don't change this line
println!("Number {}", x); println!("Number {}", x);

View File

@ -3,11 +3,13 @@
// Execute `rustlings hint variables5` or use the `hint` watch subcommand for a // Execute `rustlings hint variables5` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
fn main() { fn main() {
let number = "T-H-R-E-E"; // don't change this line let number = "T-H-R-E-E"; // don't change this line
println!("Spell a Number : {}", number); println!("Spell a Number : {}", number);
number = 3; // don't rename this variable // number = 3; // don't rename this variable
println!("Number plus two is : {}", number + 2); // Shadowing
let number = 3;
println!("Number plus two is : {}", number + 2);
} }

View File

@ -3,9 +3,11 @@
// Execute `rustlings hint variables6` or use the `hint` watch subcommand for a // Execute `rustlings hint variables6` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE // I AM DONE
const NUMBER = 3; // Constants types must also always be annotated.
// 常量的类型总是需要标注
const NUMBER: i32 = 3;
fn main() { fn main() {
println!("Number {}", NUMBER); println!("Number {}", NUMBER);
} }

View File

@ -7,11 +7,18 @@
// //
// Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint vecs1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE // I AM DONE
fn array_and_vec() -> ([i32; 4], Vec<i32>) { fn array_and_vec() -> ([i32; 4], Vec<i32>) {
let a = [10, 20, 30, 40]; // a plain array let a = [10, 20, 30, 40]; // a plain array
let v = // TODO: declare your vector here with the macro for vectors // let v = // TODO: declare your vector here with the macro for vectors
// let mut v = Vec::new();
// v.push(10);
// v.push(20);
// v.push(30);
// v.push(40);
let v = vec![10, 20, 30, 40];
(a, v) (a, v)
} }

View File

@ -13,7 +13,7 @@ fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for element in v.iter_mut() { for element in v.iter_mut() {
// TODO: Fill this up so that each element in the Vec `v` is // TODO: Fill this up so that each element in the Vec `v` is
// multiplied by 2. // multiplied by 2.
??? *element = *element * 2;
} }
// At this point, `v` should be equal to [4, 8, 12, 16, 20]. // At this point, `v` should be equal to [4, 8, 12, 16, 20].
@ -24,7 +24,7 @@ fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|element| { v.iter().map(|element| {
// TODO: Do the same thing as above - but instead of mutating the // TODO: Do the same thing as above - but instead of mutating the
// Vec, you can just return the new number! // Vec, you can just return the new number!
??? element * 2
}).collect() }).collect()
} }