mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-11 00:00:04 +03:00
Compare commits
No commits in common. "f2c3dcab3ac20e5aeddc7f792409727803da8bb8" and "d0b843d6c4a99636d3dc6caf3ceebea14cb3b07d" have entirely different histories.
f2c3dcab3a
...
d0b843d6c4
|
@ -1,5 +1,5 @@
|
||||||
fn bigger(a: i32, b: i32) -> i32 {
|
pub fn bigger(a: i32, b: i32) -> i32 {
|
||||||
// TODO: Complete this function to return the bigger number!
|
// Complete this function to return the bigger number!
|
||||||
// If both numbers are equal, any of them can be returned.
|
// If both numbers are equal, any of them can be returned.
|
||||||
// Do not use:
|
// Do not use:
|
||||||
// - another function call
|
// - another function call
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
// TODO: Fix the compiler error on this function.
|
// Step 1: Make me compile!
|
||||||
fn foo_if_fizz(fizzish: &str) -> &str {
|
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
|
||||||
|
|
||||||
|
pub fn foo_if_fizz(fizzish: &str) -> &str {
|
||||||
if fizzish == "fizz" {
|
if fizzish == "fizz" {
|
||||||
"foo"
|
"foo"
|
||||||
} else {
|
} else {
|
||||||
|
@ -11,25 +13,23 @@ fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Read the tests to understand the desired behavior.
|
// No test changes needed!
|
||||||
// Make all tests pass without changing them.
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn foo_for_fizz() {
|
fn foo_for_fizz() {
|
||||||
// This means that calling `foo_if_fizz` with the argument "fizz" should return "foo".
|
assert_eq!(foo_if_fizz("fizz"), "foo")
|
||||||
assert_eq!(foo_if_fizz("fizz"), "foo");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bar_for_fuzz() {
|
fn bar_for_fuzz() {
|
||||||
assert_eq!(foo_if_fizz("fuzz"), "bar");
|
assert_eq!(foo_if_fizz("fuzz"), "bar")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn default_to_baz() {
|
fn default_to_baz() {
|
||||||
assert_eq!(foo_if_fizz("literally anything"), "baz");
|
assert_eq!(foo_if_fizz("literally anything"), "baz")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
fn animal_habitat(animal: &str) -> &str {
|
pub fn animal_habitat(animal: &str) -> &'static str {
|
||||||
// TODO: Fix the compiler error in the statement below.
|
|
||||||
let identifier = if animal == "crab" {
|
let identifier = if animal == "crab" {
|
||||||
1
|
1
|
||||||
} else if animal == "gopher" {
|
} else if animal == "gopher" {
|
||||||
|
@ -10,8 +9,8 @@ fn animal_habitat(animal: &str) -> &str {
|
||||||
"Unknown"
|
"Unknown"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Don't change the expression below!
|
// DO NOT CHANGE THIS STATEMENT BELOW
|
||||||
if identifier == 1 {
|
let habitat = if identifier == 1 {
|
||||||
"Beach"
|
"Beach"
|
||||||
} else if identifier == 2 {
|
} else if identifier == 2 {
|
||||||
"Burrow"
|
"Burrow"
|
||||||
|
@ -19,14 +18,16 @@ fn animal_habitat(animal: &str) -> &str {
|
||||||
"Desert"
|
"Desert"
|
||||||
} else {
|
} else {
|
||||||
"Unknown"
|
"Unknown"
|
||||||
}
|
};
|
||||||
|
|
||||||
|
habitat
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't change the tests!
|
// No test changes needed.
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -14,6 +14,6 @@ mod tests {
|
||||||
|
|
||||||
let nice_slice = ???
|
let nice_slice = ???
|
||||||
|
|
||||||
assert_eq!([2, 3, 4], nice_slice);
|
assert_eq!([2, 3, 4], nice_slice)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_nametag_text(name: String) -> Option<String> {
|
pub fn generate_nametag_text(name: String) -> Option<String> {
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
// Empty names aren't allowed.
|
// Empty names aren't allowed.
|
||||||
None
|
None
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
let processing_fee = 1;
|
let processing_fee = 1;
|
||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
let qty = item_quantity.parse::<i32>();
|
let qty = item_quantity.parse::<i32>();
|
||||||
|
|
|
@ -18,7 +18,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
let processing_fee = 1;
|
let processing_fee = 1;
|
||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
let qty = item_quantity.parse::<i32>()?;
|
let qty = item_quantity.parse::<i32>()?;
|
||||||
|
|
|
@ -6,7 +6,7 @@ struct Wrapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Wrapper {
|
impl Wrapper {
|
||||||
fn new(value: u32) -> Self {
|
pub fn new(value: u32) -> Self {
|
||||||
Wrapper { value }
|
Wrapper { value }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
//
|
//
|
||||||
// Consider what you can add to the Licensed trait.
|
// Consider what you can add to the Licensed trait.
|
||||||
|
|
||||||
trait Licensed {
|
pub trait Licensed {
|
||||||
fn licensing_info(&self) -> String;
|
fn licensing_info(&self) -> String;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//
|
//
|
||||||
// Don't change any line other than the marked one.
|
// Don't change any line other than the marked one.
|
||||||
|
|
||||||
trait Licensed {
|
pub trait Licensed {
|
||||||
fn licensing_info(&self) -> String {
|
fn licensing_info(&self) -> String {
|
||||||
"some information".to_string()
|
"some information".to_string()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
//
|
//
|
||||||
// Don't change any line other than the marked one.
|
// Don't change any line other than the marked one.
|
||||||
|
|
||||||
trait SomeTrait {
|
pub trait SomeTrait {
|
||||||
fn some_function(&self) -> bool {
|
fn some_function(&self) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trait OtherTrait {
|
pub trait OtherTrait {
|
||||||
fn other_function(&self) -> bool {
|
fn other_function(&self) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// the test passes. Then write a second test that tests whether we get the
|
// the test passes. Then write a second test that tests whether we get the
|
||||||
// result we expect to get when we call `is_even(5)`.
|
// result we expect to get when we call `is_even(5)`.
|
||||||
|
|
||||||
fn is_even(num: i32) -> bool {
|
pub fn is_even(num: i32) -> bool {
|
||||||
num % 2 == 0
|
num % 2 == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ struct Rectangle {
|
||||||
|
|
||||||
impl Rectangle {
|
impl Rectangle {
|
||||||
// Only change the test functions themselves
|
// Only change the test functions themselves
|
||||||
fn new(width: i32, height: i32) -> Self {
|
pub fn new(width: i32, height: i32) -> Self {
|
||||||
if width <= 0 || height <= 0 {
|
if width <= 0 || height <= 0 {
|
||||||
panic!("Rectangle width and height cannot be negative!")
|
panic!("Rectangle width and height cannot be negative!")
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
// Step 1.
|
// Step 1.
|
||||||
// Complete the `capitalize_first` function.
|
// Complete the `capitalize_first` function.
|
||||||
// "hello" -> "Hello"
|
// "hello" -> "Hello"
|
||||||
fn capitalize_first(input: &str) -> String {
|
pub fn capitalize_first(input: &str) -> String {
|
||||||
let mut c = input.chars();
|
let mut c = input.chars();
|
||||||
match c.next() {
|
match c.next() {
|
||||||
None => String::new(),
|
None => String::new(),
|
||||||
|
@ -16,7 +16,7 @@ fn capitalize_first(input: &str) -> String {
|
||||||
// Apply the `capitalize_first` function to a slice of string slices.
|
// Apply the `capitalize_first` function to a slice of string slices.
|
||||||
// Return a vector of strings.
|
// Return a vector of strings.
|
||||||
// ["hello", "world"] -> ["Hello", "World"]
|
// ["hello", "world"] -> ["Hello", "World"]
|
||||||
fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
pub fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
vec![]
|
vec![]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ fn capitalize_words_vector(words: &[&str]) -> Vec<String> {
|
||||||
// Apply the `capitalize_first` function again to a slice of string slices.
|
// Apply the `capitalize_first` function again to a slice of string slices.
|
||||||
// Return a single string.
|
// Return a single string.
|
||||||
// ["hello", " ", "world"] -> "Hello World"
|
// ["hello", " ", "world"] -> "Hello World"
|
||||||
fn capitalize_words_string(words: &[&str]) -> String {
|
pub fn capitalize_words_string(words: &[&str]) -> String {
|
||||||
String::new()
|
String::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,20 @@
|
||||||
// list_of_results functions.
|
// list_of_results functions.
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
enum DivisionError {
|
pub enum DivisionError {
|
||||||
NotDivisible(NotDivisibleError),
|
NotDivisible(NotDivisibleError),
|
||||||
DivideByZero,
|
DivideByZero,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
struct NotDivisibleError {
|
pub struct NotDivisibleError {
|
||||||
dividend: i32,
|
dividend: i32,
|
||||||
divisor: i32,
|
divisor: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
// Calculate `a` divided by `b` if `a` is evenly divisible by `b`.
|
||||||
// Otherwise, return a suitable error.
|
// Otherwise, return a suitable error.
|
||||||
fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
pub fn divide(a: i32, b: i32) -> Result<i32, DivisionError> {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
fn factorial(num: u64) -> u64 {
|
pub fn factorial(num: u64) -> u64 {
|
||||||
// Complete this function to return the factorial of num
|
// Complete this function to return the factorial of num
|
||||||
// Do not use:
|
// Do not use:
|
||||||
// - early returns (using the `return` keyword explicitly)
|
// - early returns (using the `return` keyword explicitly)
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
// Note: the tests should not be changed
|
// Note: the tests should not be changed
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
enum List {
|
pub enum List {
|
||||||
Cons(i32, List),
|
Cons(i32, List),
|
||||||
Nil,
|
Nil,
|
||||||
}
|
}
|
||||||
|
@ -28,11 +28,11 @@ fn main() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_empty_list() -> List {
|
pub fn create_empty_list() -> List {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_non_empty_list() -> List {
|
pub fn create_non_empty_list() -> List {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,11 +42,11 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_empty_list() {
|
fn test_create_empty_list() {
|
||||||
assert_eq!(List::Nil, create_empty_list());
|
assert_eq!(List::Nil, create_empty_list())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_create_non_empty_list() {
|
fn test_create_non_empty_list() {
|
||||||
assert_ne!(create_empty_list(), create_non_empty_list());
|
assert_ne!(create_empty_list(), create_non_empty_list())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,6 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("total numbers received: {}", total_received);
|
println!("total numbers received: {}", total_received);
|
||||||
assert_eq!(total_received, queue_length);
|
assert_eq!(total_received, queue_length)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,22 +10,27 @@
|
||||||
// quantity bought.
|
// quantity bought.
|
||||||
|
|
||||||
// Put your function here!
|
// Put your function here!
|
||||||
// fn calculate_price_of_apples(???) -> ??? {
|
// fn calculate_price_of_apples {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't change the tests!
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
// Don't modify this test!
|
||||||
#[test]
|
#[test]
|
||||||
fn verify_test() {
|
fn verify_test() {
|
||||||
assert_eq!(calculate_price_of_apples(35), 70);
|
let price1 = calculate_price_of_apples(35);
|
||||||
assert_eq!(calculate_price_of_apples(40), 80);
|
let price2 = calculate_price_of_apples(40);
|
||||||
assert_eq!(calculate_price_of_apples(41), 41);
|
let price3 = calculate_price_of_apples(41);
|
||||||
assert_eq!(calculate_price_of_apples(65), 65);
|
let price4 = calculate_price_of_apples(65);
|
||||||
|
|
||||||
|
assert_eq!(70, price1);
|
||||||
|
assert_eq!(80, price2);
|
||||||
|
assert_eq!(41, price3);
|
||||||
|
assert_eq!(65, price4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
// the first element is the string, the second one is the command.
|
// the first element is the string, the second one is the command.
|
||||||
// - The output element is going to be a Vector of strings.
|
// - The output element is going to be a Vector of strings.
|
||||||
|
|
||||||
enum Command {
|
pub enum Command {
|
||||||
Uppercase,
|
Uppercase,
|
||||||
Trim,
|
Trim,
|
||||||
Append(usize),
|
Append(usize),
|
||||||
|
|
|
@ -12,14 +12,14 @@
|
||||||
// to support alphabetical report cards. Change the Grade in the second test to
|
// to support alphabetical report cards. Change the Grade in the second test to
|
||||||
// "A+" to show that your changes allow alphabetical grades.
|
// "A+" to show that your changes allow alphabetical grades.
|
||||||
|
|
||||||
struct ReportCard {
|
pub struct ReportCard {
|
||||||
grade: f32,
|
pub grade: f32,
|
||||||
student_name: String,
|
pub student_name: String,
|
||||||
student_age: u8,
|
pub student_age: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReportCard {
|
impl ReportCard {
|
||||||
fn print(&self) -> String {
|
pub fn print(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
"{} ({}) - achieved a grade of {}",
|
"{} ({}) - achieved a grade of {}",
|
||||||
&self.student_name, &self.student_age, &self.grade
|
&self.student_name, &self.student_age, &self.grade
|
||||||
|
|
|
@ -200,19 +200,17 @@ Some similar examples from other languages:
|
||||||
- In Python this would be: `a if a > b else b`
|
- In Python this would be: `a if a > b else b`
|
||||||
|
|
||||||
Remember in Rust that:
|
Remember in Rust that:
|
||||||
- The `if` condition does not need to be surrounded by parentheses
|
- the `if` condition does not need to be surrounded by parentheses
|
||||||
- `if`/`else` conditionals are expressions
|
- `if`/`else` conditionals are expressions
|
||||||
- Each condition is followed by a `{}` block"""
|
- Each condition is followed by a `{}` block."""
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "if2"
|
name = "if2"
|
||||||
dir = "03_if"
|
dir = "03_if"
|
||||||
hint = """
|
hint = """
|
||||||
For that first compiler error, it's important in Rust that each conditional
|
For that first compiler error, it's important in Rust that each conditional
|
||||||
block returns the same type!
|
block returns the same type! To get the tests passing, you will need a couple
|
||||||
|
conditions checking different input values."""
|
||||||
To get the tests passing, you will need a couple conditions checking different
|
|
||||||
input values. Read the tests to find out what they expect."""
|
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "if3"
|
name = "if3"
|
||||||
|
|
|
@ -1,32 +1 @@
|
||||||
fn bigger(a: i32, b: i32) -> i32 {
|
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||||
if a > b {
|
|
||||||
a
|
|
||||||
} else {
|
|
||||||
b
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// You can optionally experiment here.
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't mind this for now :)
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn ten_is_bigger_than_eight() {
|
|
||||||
assert_eq!(10, bigger(10, 8));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn fortytwo_is_bigger_than_thirtytwo() {
|
|
||||||
assert_eq!(42, bigger(32, 42));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn equal_numbers() {
|
|
||||||
assert_eq!(42, bigger(42, 42));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,33 +1 @@
|
||||||
fn foo_if_fizz(fizzish: &str) -> &str {
|
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||||
if fizzish == "fizz" {
|
|
||||||
"foo"
|
|
||||||
} else if fizzish == "fuzz" {
|
|
||||||
"bar"
|
|
||||||
} else {
|
|
||||||
"baz"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// You can optionally experiment here.
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn foo_for_fizz() {
|
|
||||||
assert_eq!(foo_if_fizz("fizz"), "foo");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn bar_for_fuzz() {
|
|
||||||
assert_eq!(foo_if_fizz("fuzz"), "bar");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn default_to_baz() {
|
|
||||||
assert_eq!(foo_if_fizz("literally anything"), "baz");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,53 +1 @@
|
||||||
fn animal_habitat(animal: &str) -> &str {
|
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||||
let identifier = if animal == "crab" {
|
|
||||||
1
|
|
||||||
} else if animal == "gopher" {
|
|
||||||
2
|
|
||||||
} else if animal == "snake" {
|
|
||||||
3
|
|
||||||
} else {
|
|
||||||
// Any unused identifier.
|
|
||||||
4
|
|
||||||
};
|
|
||||||
|
|
||||||
// Instead of such an identifier, you would use an enum in Rust.
|
|
||||||
// But we didn't get into enums yet.
|
|
||||||
if identifier == 1 {
|
|
||||||
"Beach"
|
|
||||||
} else if identifier == 2 {
|
|
||||||
"Burrow"
|
|
||||||
} else if identifier == 3 {
|
|
||||||
"Desert"
|
|
||||||
} else {
|
|
||||||
"Unknown"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// You can optionally experiment here.
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn gopher_lives_in_burrow() {
|
|
||||||
assert_eq!(animal_habitat("gopher"), "Burrow")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn snake_lives_in_desert() {
|
|
||||||
assert_eq!(animal_habitat("snake"), "Desert")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn crab_lives_on_beach() {
|
|
||||||
assert_eq!(animal_habitat("crab"), "Beach")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn unknown_animal() {
|
|
||||||
assert_eq!(animal_habitat("dinosaur"), "Unknown")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,31 +1 @@
|
||||||
// Mary is buying apples. The price of an apple is calculated as follows:
|
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
||||||
// - An apple costs 2 rustbucks.
|
|
||||||
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
|
|
||||||
// Write a function that calculates the price of an order of apples given the
|
|
||||||
// quantity bought.
|
|
||||||
|
|
||||||
fn calculate_price_of_apples(n_apples: u64) -> u64 {
|
|
||||||
if n_apples > 40 {
|
|
||||||
n_apples
|
|
||||||
} else {
|
|
||||||
2 * n_apples
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// You can optionally experiment here.
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't change the tests!
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn verify_test() {
|
|
||||||
assert_eq!(calculate_price_of_apples(35), 70);
|
|
||||||
assert_eq!(calculate_price_of_apples(40), 80);
|
|
||||||
assert_eq!(calculate_price_of_apples(41), 41);
|
|
||||||
assert_eq!(calculate_price_of_apples(65), 65);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue