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)]
|
||||
struct Package {
|
||||
sender_country: String,
|
||||
@@ -11,8 +8,6 @@ struct Package {
|
||||
impl Package {
|
||||
fn new(sender_country: String, recipient_country: String, weight_in_grams: u32) -> Self {
|
||||
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");
|
||||
}
|
||||
|
||||
@@ -23,15 +18,12 @@ impl Package {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add the correct return type to the function signature.
|
||||
fn is_international(&self) {
|
||||
// TODO: Read the tests that use this method to find out when a package
|
||||
// is considered international.
|
||||
fn is_international(&self) -> bool {
|
||||
self.sender_country != self.recipient_country
|
||||
}
|
||||
|
||||
// TODO: Add the correct return type to the function signature.
|
||||
fn get_fees(&self, cents_per_gram: u32) {
|
||||
// TODO: Calculate the package's fees.
|
||||
fn get_fees(&self, cents_per_gram: u32) -> u32 {
|
||||
cents_per_gram * self.weight_in_grams
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +60,6 @@ mod tests {
|
||||
let recipient_country = sender_country.clone();
|
||||
|
||||
let package = Package::new(sender_country, recipient_country, 1200);
|
||||
|
||||
assert!(!package.is_international());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#[derive(Debug)]
|
||||
enum Message {
|
||||
// TODO: Define a few types of messages as used below.
|
||||
Resize,
|
||||
Move,
|
||||
Echo,
|
||||
ChangeColor,
|
||||
Quit,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -6,7 +6,11 @@ struct Point {
|
||||
|
||||
#[derive(Debug)]
|
||||
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 {
|
||||
|
||||
@@ -44,8 +44,13 @@ impl State {
|
||||
}
|
||||
|
||||
fn process(&mut self, message: Message) {
|
||||
// TODO: Create a match expression to process the different message
|
||||
// variants using the methods defined above.
|
||||
match message {
|
||||
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.
|
||||
fn current_favorite_color() -> String {
|
||||
"blue"
|
||||
"blue".to_string()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
@@ -6,7 +6,7 @@ fn is_a_color_word(attempt: &str) -> bool {
|
||||
fn main() {
|
||||
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!");
|
||||
} else {
|
||||
println!("That is not a color word I know.");
|
||||
|
||||
Reference in New Issue
Block a user