diff --git a/exercise/exercises/structs/README.md b/exercise/exercises/structs/README.md index 3fc1fdc9..d3cea75c 100644 --- a/exercise/exercises/structs/README.md +++ b/exercise/exercises/structs/README.md @@ -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) - [Method Syntax](https://doc.rust-lang.org/book/ch05-03-method-syntax.html) diff --git a/exercise/exercises/structs/structs1.rs b/exercise/exercises/structs/structs1.rs index 6d0b2f49..f36779b4 100644 --- a/exercise/exercises/structs/structs1.rs +++ b/exercise/exercises/structs/structs1.rs @@ -1,13 +1,13 @@ // structs1.rs -// Address all the TODOs to make the tests pass! +// 解决所有的 TODO ,通过测试! // I AM NOT DONE struct ColorClassicStruct { - // TODO: Something goes here + // TODO: 一些东西需要在这里 } -struct ColorTupleStruct(/* TODO: Something goes here */); +struct ColorTupleStruct(/* TODO: 一些东西需要在这里 */); #[derive(Debug)] struct UnitStruct; @@ -18,7 +18,7 @@ mod tests { #[test] fn classic_c_structs() { - // TODO: Instantiate a classic c struct! + // TODO: 实例化一个经典的 C 结构体! // let green = assert_eq!(green.name, "green"); @@ -27,7 +27,7 @@ mod tests { #[test] fn tuple_structs() { - // TODO: Instantiate a tuple struct! + // TODO: 实例化一个元组结构! // let green = assert_eq!(green.0, "green"); @@ -36,7 +36,7 @@ mod tests { #[test] fn unit_structs() { - // TODO: Instantiate a unit struct! + // TODO: 实例化一个单元结构! // let unit_struct = let message = format!("{:?}s are fun!", unit_struct); diff --git a/exercise/exercises/structs/structs2.rs b/exercise/exercises/structs/structs2.rs index f9c6427d..dfe04176 100644 --- a/exercise/exercises/structs/structs2.rs +++ b/exercise/exercises/structs/structs2.rs @@ -1,5 +1,5 @@ // structs2.rs -// Address all the TODOs to make the tests pass! +// 解决所有的 TODO ,通过测试! // I AM NOT DONE @@ -33,7 +33,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); diff --git a/exercise/exercises/structs/structs3.rs b/exercise/exercises/structs/structs3.rs index 1a81531c..e890023e 100644 --- a/exercise/exercises/structs/structs3.rs +++ b/exercise/exercises/structs/structs3.rs @@ -1,22 +1,22 @@ // 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! -// If you have issues execute `rustlings hint structs3` +// 接口既可以包含数据也可以处理逻辑。 +// 在这个练习中,我们已经定义了 Package 结构,但我们想测试根据它实现的一些逻辑。 +// 让代码通过编译和测试! +// 如果你有问题,可以执行 `rustlings hint structs3` 查看提示 // I AM NOT DONE #[derive(Debug)] -struct Package { - sender_country: String, - recipient_country: String, - weight_in_grams: i32, +struct Package {// 译:包裹 + sender_country: String,// 译:寄件人国家 + recipient_country: String,// 译:收件人国家 + weight_in_grams: i32,// 译:重量(克) } impl Package { fn new(sender_country: String, recipient_country: String, weight_in_grams: i32) -> Package { if weight_in_grams <= 0 { - // Something goes here... + // 这里需要完成一些东西…… } else { Package { sender_country, @@ -26,12 +26,12 @@ impl Package { } } - fn is_international(&self) -> ??? { - // Something goes here... + fn is_international(&self) -> ??? {// 译:是否是国际上的 + // 这里需要完成一些东西…… } - fn get_fees(&self, cents_per_gram: i32) -> ??? { - // Something goes here... + fn get_fees(&self, cents_per_gram: i32) -> ??? {// 译:获取所需费用 + // 这里需要完成一些东西…… } } @@ -41,15 +41,15 @@ mod tests { #[test] #[should_panic] - fn fail_creating_weightless_package() { - let sender_country = String::from("Spain"); - let recipient_country = String::from("Austria"); + fn fail_creating_weightless_package() {// 译:失败地创造失重的包裹(要求不允许负重量的包裹出现) + let sender_country = String::from("Spain");// 译:西班牙 + let recipient_country = String::from("Austria");// 译:奥地利 Package::new(sender_country, recipient_country, -2210); } #[test] - fn create_international_package() { + fn create_international_package() {// 译:创建国际上的包裹 let sender_country = String::from("Spain"); let recipient_country = String::from("Russia"); @@ -69,11 +69,11 @@ mod tests { } #[test] - fn calculate_transport_fees() { + fn calculate_transport_fees() {// 译:计算运输费 let sender_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); diff --git a/exercise/info.toml b/exercise/info.toml index 8b194743..11b4893d 100644 --- a/exercise/info.toml +++ b/exercise/info.toml @@ -272,35 +272,35 @@ name = "structs1" path = "exercises/structs/structs1.rs" mode = "test" hint = """ -Rust has more than one type of struct. Three actually, all variants are used to package related data together. -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. +Rust 不只有一种结构。实际上,所有的变体都是用来组合相关的数据。 +首先是一般的(或经典的)结构,一块相关的数据被命名为一个字段集合。 +元组结构基本上就是被命名的元组。 +最后的单元结构没有任何字段,对泛型很有用。 -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]] name = "structs2" path = "exercises/structs/structs2.rs" mode = "test" 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]] name = "structs3" path = "exercises/structs/structs3.rs" mode = "test" 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