mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-31 15:59:18 +00:00
翻譯
This commit is contained in:
parent
2e8fc43a19
commit
235b3bdf50
@ -1,10 +1,10 @@
|
|||||||
# Move Semantics
|
# 移動語義(Move Semantics)
|
||||||
|
|
||||||
These exercises are adapted from [pnkfelix](https://github.com/pnkfelix)'s [Rust Tutorial](https://pnkfelix.github.io/rust-examples-icfp2014/) -- Thank you Felix!!!
|
這些練習改編自 [pnkfelix](https://github.com/pnkfelix) 的 [Rust Tutorial](https://pnkfelix.github.io/rust-examples-icfp2014/) ——感謝 Felix!!!
|
||||||
|
|
||||||
## Further information
|
## 更多資訊
|
||||||
|
|
||||||
For this section, the book links are especially important.
|
在這一部分中的連結特別重要。
|
||||||
|
|
||||||
- [Ownership](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html)
|
- [所有權](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html)
|
||||||
- [Reference and borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)
|
- [引用與借用](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
# Structs
|
# 結構體
|
||||||
|
|
||||||
Rust has three struct types: a classic C struct, a tuple struct, and a unit struct.
|
Rust 有三種結構體類型:經典 C 結構體、元組結構體和單元結構體。
|
||||||
|
|
||||||
## Further information
|
## 更多資訊
|
||||||
|
|
||||||
- [Structures](https://doc.rust-lang.org/book/ch05-01-defining-structs.html)
|
- [結構體](https://doc.rust-lang.org/book/ch05-01-defining-structs.html)
|
||||||
- [Method Syntax](https://doc.rust-lang.org/book/ch05-03-method-syntax.html)
|
- [方法語法](https://doc.rust-lang.org/book/ch05-03-method-syntax.html)
|
||||||
|
|||||||
@ -1,17 +1,16 @@
|
|||||||
// structs1.rs
|
// structs1.rs
|
||||||
//
|
//
|
||||||
// Address all the TODOs to make the tests pass!
|
// 完成所有的 TODO 項以通過測試!
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
|
// 執行 `rustlings hint structs1` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
struct ColorClassicStruct {
|
struct ColorClassicStruct {
|
||||||
// TODO: Something goes here
|
// TODO: 在此處添加內容
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ColorTupleStruct(/* TODO: Something goes here */);
|
struct ColorTupleStruct(/* TODO: 在此處添加內容 */);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitLikeStruct;
|
struct UnitLikeStruct;
|
||||||
@ -22,7 +21,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn classic_c_structs() {
|
fn classic_c_structs() {
|
||||||
// TODO: Instantiate a classic c struct!
|
// TODO: 實例化一個經典 C 結構體!
|
||||||
// let green =
|
// let green =
|
||||||
|
|
||||||
assert_eq!(green.red, 0);
|
assert_eq!(green.red, 0);
|
||||||
@ -32,7 +31,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tuple_structs() {
|
fn tuple_structs() {
|
||||||
// TODO: Instantiate a tuple struct!
|
// TODO: 實例化一個元組結構體!
|
||||||
// let green =
|
// let green =
|
||||||
|
|
||||||
assert_eq!(green.0, 0);
|
assert_eq!(green.0, 0);
|
||||||
@ -42,7 +41,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unit_structs() {
|
fn unit_structs() {
|
||||||
// TODO: Instantiate a unit-like struct!
|
// TODO: 實例化一個單元結構體!
|
||||||
// let unit_like_struct =
|
// let unit_like_struct =
|
||||||
let message = format!("{:?}s are fun!", unit_like_struct);
|
let message = format!("{:?}s are fun!", unit_like_struct);
|
||||||
|
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
// structs2.rs
|
// structs2.rs
|
||||||
//
|
//
|
||||||
// Address all the TODOs to make the tests pass!
|
// 完成所有的 TODO 項以通過測試!
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
|
// 執行 `rustlings hint structs2` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
@ -37,7 +36,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn your_order() {
|
fn your_order() {
|
||||||
let order_template = create_order_template();
|
let order_template = create_order_template();
|
||||||
// TODO: Create your own order using the update syntax and template above!
|
// TODO: 使用上述模板和更新語法創建您自己的訂單!
|
||||||
// let your_order =
|
// let your_order =
|
||||||
assert_eq!(your_order.name, "Hacker in Rust");
|
assert_eq!(your_order.name, "Hacker in Rust");
|
||||||
assert_eq!(your_order.year, order_template.year);
|
assert_eq!(your_order.year, order_template.year);
|
||||||
|
|||||||
@ -1,11 +1,9 @@
|
|||||||
// structs3.rs
|
// structs3.rs
|
||||||
//
|
//
|
||||||
// Structs contain data, but can also have logic. In this exercise we have
|
// 結構體包含數據,但也可以包含邏輯。在這個練習中,我們定義了 Package 結構體,並且我們希望測試與其相關的一些邏輯。
|
||||||
// defined the Package struct and we want to test some logic attached to it.
|
// 讓代碼編譯並通過測試!
|
||||||
// Make the code compile and the tests pass!
|
|
||||||
//
|
//
|
||||||
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
|
// 執行 `rustlings hint structs3` 或使用 `hint` watch 子命令來獲取提示。
|
||||||
// hint.
|
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
@ -19,9 +17,9 @@ struct Package {
|
|||||||
impl Package {
|
impl Package {
|
||||||
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Package {
|
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Package {
|
||||||
if weight_in_grams < 10 {
|
if weight_in_grams < 10 {
|
||||||
// This is not how you should handle errors in Rust,
|
// 這不是在 Rust 中處理錯誤的方式,
|
||||||
// but we will learn about error handling later.
|
// 但我們稍後會學習錯誤處理。
|
||||||
panic!("Can not ship a package with weight below 10 grams.")
|
panic!("不能運送重量低於 10 克的包裹。")
|
||||||
} else {
|
} else {
|
||||||
Package {
|
Package {
|
||||||
sender_country,
|
sender_country,
|
||||||
@ -32,11 +30,11 @@ impl Package {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_international(&self) -> ??? {
|
fn is_international(&self) -> ??? {
|
||||||
// Something goes here...
|
// 在這裡填寫內容...
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_fees(&self, cents_per_gram: u32) -> ??? {
|
fn get_fees(&self, cents_per_gram: u32) -> ??? {
|
||||||
// Something goes here...
|
// 在這裡填寫內容...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,16 +45,16 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn fail_creating_weightless_package() {
|
fn fail_creating_weightless_package() {
|
||||||
let sender_country = String::from("Spain");
|
let sender_country = String::from("西班牙");
|
||||||
let recipient_country = String::from("Austria");
|
let recipient_country = String::from("奧地利");
|
||||||
|
|
||||||
Package::new(sender_country, recipient_country, 5);
|
Package::new(sender_country, recipient_country, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_international_package() {
|
fn create_international_package() {
|
||||||
let sender_country = String::from("Spain");
|
let sender_country = String::from("西班牙");
|
||||||
let recipient_country = String::from("Russia");
|
let recipient_country = String::from("俄羅斯");
|
||||||
|
|
||||||
let package = Package::new(sender_country, recipient_country, 1200);
|
let package = Package::new(sender_country, recipient_country, 1200);
|
||||||
|
|
||||||
@ -65,7 +63,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_local_package() {
|
fn create_local_package() {
|
||||||
let sender_country = String::from("Canada");
|
let sender_country = String::from("加拿大");
|
||||||
let recipient_country = sender_country.clone();
|
let recipient_country = sender_country.clone();
|
||||||
|
|
||||||
let package = Package::new(sender_country, recipient_country, 1200);
|
let package = Package::new(sender_country, recipient_country, 1200);
|
||||||
@ -75,8 +73,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn calculate_transport_fees() {
|
fn calculate_transport_fees() {
|
||||||
let sender_country = String::from("Spain");
|
let sender_country = String::from("西班牙");
|
||||||
let recipient_country = String::from("Spain");
|
let recipient_country = String::from("西班牙");
|
||||||
|
|
||||||
let cents_per_gram = 3;
|
let cents_per_gram = 3;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user