mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-28 00:00:03 +03:00
Complete structs exercise
This commit is contained in:
parent
fe724b0437
commit
4b14488a4b
|
@ -5,13 +5,14 @@
|
||||||
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct ColorClassicStruct {
|
struct ColorClassicStruct {
|
||||||
// TODO: Something goes here
|
// TODO: Something goes here
|
||||||
|
red: i32,
|
||||||
|
green: i32,
|
||||||
|
blue: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ColorTupleStruct(/* TODO: Something goes here */);
|
struct ColorTupleStruct(i32, i32, i32);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitLikeStruct;
|
struct UnitLikeStruct;
|
||||||
|
@ -23,7 +24,11 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn classic_c_structs() {
|
fn classic_c_structs() {
|
||||||
// TODO: Instantiate a classic c struct!
|
// TODO: Instantiate a classic c struct!
|
||||||
// let green =
|
let green = ColorClassicStruct {
|
||||||
|
red: 0,
|
||||||
|
green: 255,
|
||||||
|
blue: 0
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(green.red, 0);
|
assert_eq!(green.red, 0);
|
||||||
assert_eq!(green.green, 255);
|
assert_eq!(green.green, 255);
|
||||||
|
@ -33,7 +38,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn tuple_structs() {
|
fn tuple_structs() {
|
||||||
// TODO: Instantiate a tuple struct!
|
// TODO: Instantiate a tuple struct!
|
||||||
// let green =
|
let green = ColorTupleStruct(0, 255, 0);
|
||||||
|
|
||||||
assert_eq!(green.0, 0);
|
assert_eq!(green.0, 0);
|
||||||
assert_eq!(green.1, 255);
|
assert_eq!(green.1, 255);
|
||||||
|
@ -43,7 +48,10 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn unit_structs() {
|
fn unit_structs() {
|
||||||
// TODO: Instantiate a unit-like struct!
|
// TODO: Instantiate a unit-like struct!
|
||||||
// let unit_like_struct =
|
// does not hold data but can be used to improve type safety
|
||||||
|
// best used when you don't need data but you want to define methods
|
||||||
|
// Great example of this is errors.
|
||||||
|
let unit_like_struct = UnitLikeStruct;
|
||||||
let message = format!("{:?}s are fun!", unit_like_struct);
|
let message = format!("{:?}s are fun!", unit_like_struct);
|
||||||
|
|
||||||
assert_eq!(message, "UnitLikeStructs are fun!");
|
assert_eq!(message, "UnitLikeStructs are fun!");
|
||||||
|
|
|
@ -5,8 +5,6 @@
|
||||||
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint structs2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Order {
|
struct Order {
|
||||||
name: String,
|
name: String,
|
||||||
|
@ -38,7 +36,15 @@ mod tests {
|
||||||
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: Create your own order using the update syntax and template above!
|
||||||
// let your_order =
|
let your_order = Order {
|
||||||
|
name: "Hacker in Rust".to_string(),
|
||||||
|
year: order_template.year,
|
||||||
|
made_by_phone: order_template.made_by_phone,
|
||||||
|
made_by_mobile: order_template.made_by_mobile,
|
||||||
|
made_by_email: order_template.made_by_email,
|
||||||
|
item_number: order_template.item_number,
|
||||||
|
count: 1
|
||||||
|
};
|
||||||
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);
|
||||||
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
|
assert_eq!(your_order.made_by_phone, order_template.made_by_phone);
|
||||||
|
|
|
@ -7,8 +7,6 @@
|
||||||
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Package {
|
struct Package {
|
||||||
sender_country: String,
|
sender_country: String,
|
||||||
|
@ -31,12 +29,16 @@ impl Package {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_international(&self) -> ??? {
|
fn is_international(&self) -> bool {
|
||||||
// Something goes here...
|
if self.sender_country == self.recipient_country {
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_fees(&self, cents_per_gram: u32) -> ??? {
|
fn get_fees(&self, cents_per_gram: u32) -> u32 {
|
||||||
// Something goes here...
|
self.weight_in_grams * cents_per_gram
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue