This commit is contained in:
TimLai666 2024-06-10 18:09:39 +08:00 committed by GitHub
parent 2e8fc43a19
commit 235b3bdf50
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 36 additions and 40 deletions

View File

@ -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)
- [Reference and borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)
- [所有權](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html)
- [引用與借用](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)

View File

@ -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)
- [Method Syntax](https://doc.rust-lang.org/book/ch05-03-method-syntax.html)
- [結構體](https://doc.rust-lang.org/book/ch05-01-defining-structs.html)
- [方法語法](https://doc.rust-lang.org/book/ch05-03-method-syntax.html)

View File

@ -1,17 +1,16 @@
// structs1.rs
//
// Address all the TODOs to make the tests pass!
// 完成所有的 TODO 項以通過測試!
//
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint structs1` 或使用 `hint` watch 子命令來獲取提示。
// I AM NOT DONE
struct ColorClassicStruct {
// TODO: Something goes here
// TODO: 在此處添加內容
}
struct ColorTupleStruct(/* TODO: Something goes here */);
struct ColorTupleStruct(/* TODO: 在此處添加內容 */);
#[derive(Debug)]
struct UnitLikeStruct;
@ -22,7 +21,7 @@ mod tests {
#[test]
fn classic_c_structs() {
// TODO: Instantiate a classic c struct!
// TODO: 實例化一個經典 C 結構體!
// let green =
assert_eq!(green.red, 0);
@ -32,7 +31,7 @@ mod tests {
#[test]
fn tuple_structs() {
// TODO: Instantiate a tuple struct!
// TODO: 實例化一個元組結構體!
// let green =
assert_eq!(green.0, 0);
@ -42,7 +41,7 @@ mod tests {
#[test]
fn unit_structs() {
// TODO: Instantiate a unit-like struct!
// TODO: 實例化一個單元結構體!
// let unit_like_struct =
let message = format!("{:?}s are fun!", unit_like_struct);

View File

@ -1,9 +1,8 @@
// structs2.rs
//
// Address all the TODOs to make the tests pass!
// 完成所有的 TODO 項以通過測試!
//
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint structs2` 或使用 `hint` watch 子命令來獲取提示。
// I AM NOT DONE
@ -37,7 +36,7 @@ mod tests {
#[test]
fn your_order() {
let order_template = create_order_template();
// TODO: Create your own order using the update syntax and template above!
// TODO: 使用上述模板和更新語法創建您自己的訂單!
// let your_order =
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);

View File

@ -1,11 +1,9 @@
// structs3.rs
//
// Structs contain data, but can also have logic. In this 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!
// 結構體包含數據,但也可以包含邏輯。在這個練習中,我們定義了 Package 結構體,並且我們希望測試與其相關的一些邏輯。
// 讓代碼編譯並通過測試!
//
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
// hint.
// 執行 `rustlings hint structs3` 或使用 `hint` watch 子命令來獲取提示。
// I AM NOT DONE
@ -19,9 +17,9 @@ struct Package {
impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Package {
if weight_in_grams < 10 {
// This is not how you should handle errors in Rust,
// but we will learn about error handling later.
panic!("Can not ship a package with weight below 10 grams.")
// 這不是在 Rust 中處理錯誤的方式,
// 但我們稍後會學習錯誤處理。
panic!("不能運送重量低於 10 克的包裹。")
} else {
Package {
sender_country,
@ -32,11 +30,11 @@ impl Package {
}
fn is_international(&self) -> ??? {
// Something goes here...
// 在這裡填寫內容...
}
fn get_fees(&self, cents_per_gram: u32) -> ??? {
// Something goes here...
// 在這裡填寫內容...
}
}
@ -47,16 +45,16 @@ mod tests {
#[test]
#[should_panic]
fn fail_creating_weightless_package() {
let sender_country = String::from("Spain");
let recipient_country = String::from("Austria");
let sender_country = String::from("西班牙");
let recipient_country = String::from("奧地利");
Package::new(sender_country, recipient_country, 5);
}
#[test]
fn create_international_package() {
let sender_country = String::from("Spain");
let recipient_country = String::from("Russia");
let sender_country = String::from("西班牙");
let recipient_country = String::from("俄羅斯");
let package = Package::new(sender_country, recipient_country, 1200);
@ -65,7 +63,7 @@ mod tests {
#[test]
fn create_local_package() {
let sender_country = String::from("Canada");
let sender_country = String::from("加拿大");
let recipient_country = sender_country.clone();
let package = Package::new(sender_country, recipient_country, 1200);
@ -75,8 +73,8 @@ mod tests {
#[test]
fn calculate_transport_fees() {
let sender_country = String::from("Spain");
let recipient_country = String::from("Spain");
let sender_country = String::from("西班牙");
let recipient_country = String::from("西班牙");
let cents_per_gram = 3;