mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-01 16:29:18 +00:00
翻譯
This commit is contained in:
parent
0f30af3e3b
commit
1b767ffd5b
@ -1,7 +1,7 @@
|
|||||||
# If
|
# If
|
||||||
|
|
||||||
`if`, the most basic (but still surprisingly versatile!) type of control flow, is what you'll learn here.
|
`if` 是最基本(但仍然非常多用途!)的控制流類型,您將在這裡學習它。
|
||||||
|
|
||||||
## Further information
|
## 更多資訊
|
||||||
|
|
||||||
- [Control Flow - if expressions](https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions)
|
- [控制流 - if 表達式](https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions)
|
||||||
|
|||||||
@ -1,18 +1,18 @@
|
|||||||
// if1.rs
|
// if1.rs
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.
|
// 執行 `rustlings hint if1` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT 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!
|
// 完成此函數以返回較大的數字!
|
||||||
// If both numbers are equal, any of them can be returned.
|
// 如果兩個數字相等,可以返回任意一個。
|
||||||
// Do not use:
|
// 不要使用:
|
||||||
// - another function call
|
// - 其他函數調用
|
||||||
// - additional variables
|
// - 其他變數
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't mind this for now :)
|
// 暫時不用管這個 :)
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
// if2.rs
|
// if2.rs
|
||||||
//
|
//
|
||||||
// Step 1: Make me compile!
|
// 第一步:讓我編譯通過!
|
||||||
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
|
// 第二步:讓 bar_for_fuzz 和 default_to_baz 測試通過!
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.
|
// 執行 `rustlings hint if2` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ pub fn foo_if_fizz(fizzish: &str) -> &str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No test changes needed!
|
// 不需要更改測試!
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
@ -1,56 +1,56 @@
|
|||||||
// if3.rs
|
// if3.rs
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.
|
// 執行 `rustlings hint if3` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT 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 == "螃蟹" {
|
||||||
1
|
1
|
||||||
} else if animal == "gopher" {
|
} else if animal == "地鼠" {
|
||||||
2.0
|
2.0
|
||||||
} else if animal == "snake" {
|
} else if animal == "蛇" {
|
||||||
3
|
3
|
||||||
} else {
|
} else {
|
||||||
"Unknown"
|
"未知"
|
||||||
};
|
};
|
||||||
|
|
||||||
// DO NOT CHANGE THIS STATEMENT BELOW
|
// 請勿更改下面的這條語句
|
||||||
let habitat = if identifier == 1 {
|
let habitat = if identifier == 1 {
|
||||||
"Beach"
|
"海灘"
|
||||||
} else if identifier == 2 {
|
} else if identifier == 2 {
|
||||||
"Burrow"
|
"地洞"
|
||||||
} else if identifier == 3 {
|
} else if identifier == 3 {
|
||||||
"Desert"
|
"沙漠"
|
||||||
} else {
|
} else {
|
||||||
"Unknown"
|
"未知"
|
||||||
};
|
};
|
||||||
|
|
||||||
habitat
|
habitat
|
||||||
}
|
}
|
||||||
|
|
||||||
// No test changes needed.
|
// 不需要更改測試。
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn gopher_lives_in_burrow() {
|
fn gopher_lives_in_burrow() {
|
||||||
assert_eq!(animal_habitat("gopher"), "Burrow")
|
assert_eq!(animal_habitat("地鼠"), "地洞")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn snake_lives_in_desert() {
|
fn snake_lives_in_desert() {
|
||||||
assert_eq!(animal_habitat("snake"), "Desert")
|
assert_eq!(animal_habitat("蛇"), "沙漠")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn crab_lives_on_beach() {
|
fn crab_lives_on_beach() {
|
||||||
assert_eq!(animal_habitat("crab"), "Beach")
|
assert_eq!(animal_habitat("螃蟹"), "海灘")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unknown_animal() {
|
fn unknown_animal() {
|
||||||
assert_eq!(animal_habitat("dinosaur"), "Unknown")
|
assert_eq!(animal_habitat("恐龍"), "未知")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
# Primitive Types
|
# 原始類型
|
||||||
|
|
||||||
Rust has a couple of basic types that are directly implemented into the
|
Rust 有幾種基本類型是直接由編譯器實現的。在這一部分,我們將介紹最重要的那些。
|
||||||
compiler. In this section, we'll go through the most important ones.
|
|
||||||
|
|
||||||
## Further information
|
## 更多資訊
|
||||||
|
|
||||||
- [Data Types](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html)
|
- [資料型態](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html)
|
||||||
- [The Slice Type](https://doc.rust-lang.org/stable/book/ch04-03-slices.html)
|
- [切片類型](https://doc.rust-lang.org/stable/book/ch04-03-slices.html)
|
||||||
|
|||||||
@ -1,20 +1,19 @@
|
|||||||
// primitive_types1.rs
|
// primitive_types1.rs
|
||||||
//
|
//
|
||||||
// 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
|
// I AM NOT DONE
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Booleans (`bool`)
|
// 布林值 (`bool`)
|
||||||
|
|
||||||
let is_morning = true;
|
let is_morning = true;
|
||||||
if is_morning {
|
if is_morning {
|
||||||
println!("Good morning!");
|
println!("早安!");
|
||||||
}
|
}
|
||||||
|
|
||||||
let // Finish the rest of this line like the example! Or make it be false!
|
let is_evening = false; // 像例子一樣完成這行!或者讓它變成 false!
|
||||||
if is_evening {
|
if is_evening {
|
||||||
println!("Good evening!");
|
println!("晚安!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,32 +1,28 @@
|
|||||||
// primitive_types2.rs
|
// primitive_types2.rs
|
||||||
//
|
//
|
||||||
// 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
|
// I AM NOT DONE
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Characters (`char`)
|
// 字符 (`char`)
|
||||||
|
|
||||||
// Note the _single_ quotes, these are different from the double quotes
|
// 注意是 _單引號_,這與您經常看到的雙引號不同。
|
||||||
// you've been seeing around.
|
|
||||||
let my_first_initial = 'C';
|
let my_first_initial = 'C';
|
||||||
if my_first_initial.is_alphabetic() {
|
if my_first_initial.is_alphabetic() {
|
||||||
println!("Alphabetical!");
|
println!("字母!");
|
||||||
} else if my_first_initial.is_numeric() {
|
} else if my_first_initial.is_numeric() {
|
||||||
println!("Numerical!");
|
println!("數字!");
|
||||||
} else {
|
} else {
|
||||||
println!("Neither alphabetic nor numeric!");
|
println!("既不是字母也不是數字!");
|
||||||
}
|
}
|
||||||
|
|
||||||
let // Finish this line like the example! What's your favorite character?
|
let // 像例子一樣完成這行!您最喜歡的字符是什麼?試試字母、數字、特殊字符,或者不同語言的字符,甚至是表情符號!
|
||||||
// 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() {
|
if your_character.is_alphabetic() {
|
||||||
println!("Alphabetical!");
|
println!("字母!");
|
||||||
} else if your_character.is_numeric() {
|
} else if your_character.is_numeric() {
|
||||||
println!("Numerical!");
|
println!("數字!");
|
||||||
} else {
|
} else {
|
||||||
println!("Neither alphabetic nor numeric!");
|
println!("既不是字母也不是數字!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
// primitive_types3.rs
|
// primitive_types3.rs
|
||||||
//
|
//
|
||||||
// Create an array with at least 100 elements in it where the ??? is.
|
// 創建一個包含至少 100 個元素的陣列,並填寫 ??? 處的代碼。
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint primitive_types3` or use the `hint` watch subcommand
|
// 執行 `rustlings hint primitive_types3` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
@ -11,9 +10,9 @@ fn main() {
|
|||||||
let a = ???
|
let a = ???
|
||||||
|
|
||||||
if a.len() >= 100 {
|
if a.len() >= 100 {
|
||||||
println!("Wow, that's a big array!");
|
println!("哇,那是一個大陣列!");
|
||||||
} else {
|
} else {
|
||||||
println!("Meh, I eat arrays like that for breakfast.");
|
println!("嗯,我早餐吃的就是這樣的陣列。");
|
||||||
panic!("Array not big enough, more elements needed")
|
panic!("陣列不夠大,需要更多元素")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
// primitive_types4.rs
|
// primitive_types4.rs
|
||||||
//
|
//
|
||||||
// Get a slice out of Array a where the ??? is so that the test passes.
|
// 從陣列 a 中獲取一個切片,填寫 ??? 處的代碼,使測試通過。
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand
|
// 執行 `rustlings hint primitive_types4` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
|||||||
@ -1,15 +1,14 @@
|
|||||||
// primitive_types5.rs
|
// primitive_types5.rs
|
||||||
//
|
//
|
||||||
// Destructure the `cat` tuple so that the println will work.
|
// 解構 `cat` 元組,使 println 可以正常工作。
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint primitive_types5` or use the `hint` watch subcommand
|
// 執行 `rustlings hint primitive_types5` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cat = ("Furry McFurson", 3.5);
|
let cat = ("Furry McFurson", 3.5);
|
||||||
let /* your pattern here */ = cat;
|
let /* 在此填寫解構模式 */ = cat;
|
||||||
|
|
||||||
println!("{} is {} years old.", name, age);
|
println!("{} is {} years old.", name, age);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,19 +1,17 @@
|
|||||||
// primitive_types6.rs
|
// primitive_types6.rs
|
||||||
//
|
//
|
||||||
// Use a tuple index to access the second element of `numbers`. You can put the
|
// 使用元組索引來訪問 `numbers` 的第二個元素。您可以將表達式替換到 ??? 處,使測試通過。
|
||||||
// expression for the second element where ??? is so that the test passes.
|
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand
|
// 執行 `rustlings hint primitive_types6` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT 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.
|
// 使用元組索引語法替換下面的 ???。
|
||||||
let second = ???;
|
let second = ???;
|
||||||
|
|
||||||
assert_eq!(2, second,
|
assert_eq!(2, second,
|
||||||
"This is not the 2nd number in the tuple!")
|
"這不是元組中的第二個數字!")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user