mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 05:39:19 +00:00
Structs
This commit is contained in:
parent
9750ddaf6a
commit
0cdeee37dd
@ -2,16 +2,15 @@
|
|||||||
// Address all the TODOs to make the tests pass!
|
// Address all the TODOs to make the tests pass!
|
||||||
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint structs1` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
struct ColorClassicStruct {
|
struct ColorClassicStruct {
|
||||||
// TODO: Something goes here
|
name: String,
|
||||||
|
hex: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ColorTupleStruct(/* TODO: Something goes here */);
|
struct ColorTupleStruct(String, String);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct UnitLikeStruct;
|
struct UnitStruct;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@ -19,30 +18,31 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn classic_c_structs() {
|
fn classic_c_structs() {
|
||||||
// TODO: Instantiate a classic c struct!
|
let green = ColorClassicStruct {
|
||||||
// let green =
|
name: String::from("green"),
|
||||||
|
hex: String::from("#00FF00"),
|
||||||
|
};
|
||||||
|
|
||||||
assert_eq!(green.red, 0);
|
assert_eq!(green.name, "green");
|
||||||
assert_eq!(green.green, 255);
|
assert_eq!(green.hex, "#00FF00");
|
||||||
assert_eq!(green.blue, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tuple_structs() {
|
fn tuple_structs() {
|
||||||
// TODO: Instantiate a tuple struct!
|
let green = ColorTupleStruct(
|
||||||
// let green =
|
String::from("green"),
|
||||||
|
String::from("#00FF00"),
|
||||||
|
);
|
||||||
|
|
||||||
assert_eq!(green.0, 0);
|
assert_eq!(green.0, "green");
|
||||||
assert_eq!(green.1, 255);
|
assert_eq!(green.1, "#00FF00");
|
||||||
assert_eq!(green.2, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unit_structs() {
|
fn unit_structs() {
|
||||||
// TODO: Instantiate a unit-like struct!
|
let unit_struct = UnitStruct;
|
||||||
// let unit_like_struct =
|
let message = format!("{:?}s are fun!", unit_struct);
|
||||||
let message = format!("{:?}s are fun!", unit_like_struct);
|
|
||||||
|
|
||||||
assert_eq!(message, "UnitLikeStructs are fun!");
|
assert_eq!(message, "UnitStructs are fun!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,7 +35,11 @@ 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: String::from("Hacker in Rust"),
|
||||||
|
count: 1,
|
||||||
|
..order_template
|
||||||
|
};
|
||||||
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);
|
||||||
|
|||||||
@ -4,8 +4,6 @@
|
|||||||
// Make the code compile and the tests pass!
|
// Make the code compile and the tests pass!
|
||||||
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint structs3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Package {
|
struct Package {
|
||||||
sender_country: String,
|
sender_country: String,
|
||||||
@ -26,12 +24,12 @@ impl Package {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_international(&self) -> ??? {
|
fn is_international(&self) -> bool {
|
||||||
// Something goes here...
|
self.sender_country != self.recipient_country
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_fees(&self, cents_per_gram: i32) -> ??? {
|
fn get_fees(&self, cents_per_gram: i32) -> i32 {
|
||||||
// Something goes here...
|
(self.weight_in_grams * cents_per_gram + (1000 / 2)) / 1000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +71,7 @@ mod tests {
|
|||||||
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 = 3000;
|
||||||
|
|
||||||
let package = Package::new(sender_country, recipient_country, 1500);
|
let package = Package::new(sender_country, recipient_country, 1500);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user