structs and enums
This commit is contained in:
@@ -1,6 +1,3 @@
|
|||||||
// 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.
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Package {
|
struct Package {
|
||||||
sender_country: String,
|
sender_country: String,
|
||||||
@@ -11,8 +8,6 @@ struct Package {
|
|||||||
impl Package {
|
impl Package {
|
||||||
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Self {
|
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Self {
|
||||||
if weight_in_grams < 10 {
|
if weight_in_grams < 10 {
|
||||||
// This isn't how you should handle errors in Rust, but we will
|
|
||||||
// learn about error handling later.
|
|
||||||
panic!("Can't ship a package with weight below 10 grams");
|
panic!("Can't ship a package with weight below 10 grams");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,15 +18,12 @@ impl Package {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add the correct return type to the function signature.
|
fn is_international(&self) -> bool {
|
||||||
fn is_international(&self) {
|
self.sender_country != self.recipient_country
|
||||||
// TODO: Read the tests that use this method to find out when a package
|
|
||||||
// is considered international.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add the correct return type to the function signature.
|
fn get_fees(&self, cents_per_gram: u32) -> u32 {
|
||||||
fn get_fees(&self, cents_per_gram: u32) {
|
cents_per_gram * self.weight_in_grams
|
||||||
// TODO: Calculate the package's fees.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +60,6 @@ mod tests {
|
|||||||
let recipient_country = sender_country.clone();
|
let recipient_country = sender_country.clone();
|
||||||
|
|
||||||
let package = Package::new(sender_country, recipient_country, 1200);
|
let package = Package::new(sender_country, recipient_country, 1200);
|
||||||
|
|
||||||
assert!(!package.is_international());
|
assert!(!package.is_international());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: Define a few types of messages as used below.
|
Resize,
|
||||||
|
Move,
|
||||||
|
Echo,
|
||||||
|
ChangeColor,
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -6,7 +6,11 @@ struct Point {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Message {
|
enum Message {
|
||||||
// TODO: Define the different variants used below.
|
Resize { width: u64, height: u64 },
|
||||||
|
Move(Point),
|
||||||
|
Echo(String),
|
||||||
|
ChangeColor(u8, u8, u8),
|
||||||
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@@ -44,8 +44,13 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn process(&mut self, message: Message) {
|
fn process(&mut self, message: Message) {
|
||||||
// TODO: Create a match expression to process the different message
|
match message {
|
||||||
// variants using the methods defined above.
|
Message::Resize { width, height } => self.resize(width, height),
|
||||||
|
Message::Move(point) => self.move_position(point),
|
||||||
|
Message::Echo(string) => self.echo(string),
|
||||||
|
Message::Quit => self.quit(),
|
||||||
|
Message::ChangeColor(r, g, b) => self.change_color(r, g, b),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// TODO: Fix the compiler error without changing the function signature.
|
// TODO: Fix the compiler error without changing the function signature.
|
||||||
fn current_favorite_color() -> String {
|
fn current_favorite_color() -> String {
|
||||||
"blue"
|
"blue".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ fn is_a_color_word(attempt: &str) -> bool {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let word = String::from("green"); // Don't change this line.
|
let word = String::from("green"); // Don't change this line.
|
||||||
|
|
||||||
if is_a_color_word(word) {
|
if is_a_color_word(&word) {
|
||||||
println!("That is a color word I know!");
|
println!("That is a color word I know!");
|
||||||
} else {
|
} else {
|
||||||
println!("That is not a color word I know.");
|
println!("That is not a color word I know.");
|
||||||
|
|||||||
Reference in New Issue
Block a user