update: translate structs

pull/134/head
mg-chao 3 years ago
parent 07086be9b8
commit 844523697a

@ -1,8 +1,8 @@
# Structs # 结构(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) - [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) - [Method Syntax](https://doc.rust-lang.org/book/ch05-03-method-syntax.html)

@ -1,13 +1,13 @@
// structs1.rs // structs1.rs
// Address all the TODOs to make the tests pass! // 解决所有的 TODO ,通过测试!
// 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 UnitStruct; struct UnitStruct;
@ -18,7 +18,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.name, "green"); assert_eq!(green.name, "green");
@ -27,7 +27,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, "green"); assert_eq!(green.0, "green");
@ -36,7 +36,7 @@ mod tests {
#[test] #[test]
fn unit_structs() { fn unit_structs() {
// TODO: Instantiate a unit struct! // TODO: 实例化一个单元结构!
// let unit_struct = // let unit_struct =
let message = format!("{:?}s are fun!", unit_struct); let message = format!("{:?}s are fun!", unit_struct);

@ -1,5 +1,5 @@
// structs2.rs // structs2.rs
// Address all the TODOs to make the tests pass! // 解决所有的 TODO ,通过测试!
// I AM NOT DONE // I AM NOT DONE
@ -33,7 +33,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,22 +1,22 @@
// structs3.rs // 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. // 在这个练习中,我们已经定义了 Package 结构,但我们想测试根据它实现的一些逻辑。
// Make the code compile and the tests pass! // 让代码通过编译和测试!
// If you have issues execute `rustlings hint structs3` // 如果你有问题,可以执行 `rustlings hint structs3` 查看提示
// I AM NOT DONE // I AM NOT DONE
#[derive(Debug)] #[derive(Debug)]
struct Package { struct Package {// 译:包裹
sender_country: String, sender_country: String,// 译:寄件人国家
recipient_country: String, recipient_country: String,// 译:收件人国家
weight_in_grams: i32, weight_in_grams: i32,// 译:重量(克)
} }
impl Package { impl Package {
fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package {
if weight_in_grams <= 0 { if weight_in_grams <= 0 {
// Something goes here... // 这里需要完成一些东西……
} else { } else {
Package { Package {
sender_country, sender_country,
@ -26,12 +26,12 @@ impl Package {
} }
} }
fn is_international(&self) -> ??? { fn is_international(&self) -> ??? {// 译:是否是国际上的
// Something goes here... // 这里需要完成一些东西……
} }
fn get_fees(&self, cents_per_gram: i32) -> ??? { fn get_fees(&self, cents_per_gram: i32) -> ??? {// 译:获取所需费用
// Something goes here... // 这里需要完成一些东西……
} }
} }
@ -41,15 +41,15 @@ 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("Spain");// 译:西班牙
let recipient_country = String::from("Austria"); let recipient_country = String::from("Austria");// 译:奥地利
Package::new(sender_country, recipient_country, -2210); Package::new(sender_country, recipient_country, -2210);
} }
#[test] #[test]
fn create_international_package() { fn create_international_package() {// 译:创建国际上的包裹
let sender_country = String::from("Spain"); let sender_country = String::from("Spain");
let recipient_country = String::from("Russia"); let recipient_country = String::from("Russia");
@ -69,11 +69,11 @@ mod tests {
} }
#[test] #[test]
fn calculate_transport_fees() { fn calculate_transport_fees() {// 译:计算运输费
let sender_country = String::from("Spain"); let sender_country = String::from("Spain");
let recipient_country = String::from("Spain"); let recipient_country = String::from("Spain");
let cents_per_gram = 3; let cents_per_gram = 3;// 译:分/克(一克需要多少分钱)
let package = Package::new(sender_country, recipient_country, 1500); let package = Package::new(sender_country, recipient_country, 1500);

@ -272,35 +272,35 @@ name = "structs1"
path = "exercises/structs/structs1.rs" path = "exercises/structs/structs1.rs"
mode = "test" mode = "test"
hint = """ hint = """
Rust has more than one type of struct. Three actually, all variants are used to package related data together. Rust
There are normal (or classic) structs. These are named collections of related data stored in fields.
Tuple structs are basically just named tuples.
Finally, Unit structs. These don't have any fields and are useful for generics.
In this exercise you need to complete and implement one of each kind.
Read more about structs in The Book: https://doc.rust-lang.org/book/ch05-01-defining-structs.html""" https://doc.rust-lang.org/book/ch05-01-defining-structs.html"""
[[exercises]] [[exercises]]
name = "structs2" name = "structs2"
path = "exercises/structs/structs2.rs" path = "exercises/structs/structs2.rs"
mode = "test" mode = "test"
hint = """ hint = """
Creating instances of structs is easy, all you need to do is assign some values to its fields.
There are however some shortcuts that can be taken when instantiating structs.
Have a look in The Book, to find out more: https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax""" https://doc.rust-lang.org/stable/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax"""
[[exercises]] [[exercises]]
name = "structs3" name = "structs3"
path = "exercises/structs/structs3.rs" path = "exercises/structs/structs3.rs"
mode = "test" mode = "test"
hint = """ hint = """
The new method needs to panic if the weight is physically impossible :), how do we do that in Rust? Package new weight_in_grams panic :) Rust
For is_international: What makes a package international? Seems related to the places it goes through right? is_international
For calculate_transport_fees: Bigger is more expensive usually, we don't have size, but something may fit the bill here :) calculate_transport_fees Package 西 :)
Have a look in The Book, to find out more about method implementations: https://doc.rust-lang.org/book/ch05-03-method-syntax.html""" https://doc.rust-lang.org/book/ch05-03-method-syntax.html"""
# ENUMS # ENUMS

Loading…
Cancel
Save