pull/136/head
sunface 3 years ago
commit 0b64dc81f8

@ -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)

@ -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);

@ -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: 利用上面的模板template 是模板的意思)然后改变其中的一些值来创建属于你的订单!
// let your_order =
assert_eq!(your_order.name, "Hacker in Rust");
assert_eq!(your_order.year, order_template.year);

@ -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);

@ -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

Loading…
Cancel
Save