initial commit
This commit is contained in:
10
exercises/02_basic_calculator/04_panics/Cargo.toml
Normal file
10
exercises/02_basic_calculator/04_panics/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "panics"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lints.rust]
|
||||
# We silence dead code warnings for the time being in order to reduce
|
||||
# compiler noise.
|
||||
# We'll re-enable them again once we explain how visibility works in Rust.
|
||||
dead_code = "allow"
|
||||
26
exercises/02_basic_calculator/04_panics/src/lib.rs
Normal file
26
exercises/02_basic_calculator/04_panics/src/lib.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
/// Given the start and end points of a journey, and the time it took to complete the journey,
|
||||
/// calculate the average speed of the journey.
|
||||
fn speed(start: u32, end: u32, time_elapsed: u32) -> u32 {
|
||||
// TODO: Panic with a custom message if `time_elapsed` is 0
|
||||
|
||||
(end - start) / time_elapsed
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::speed;
|
||||
|
||||
#[test]
|
||||
fn case1() {
|
||||
assert_eq!(speed(0, 10, 10), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
// 👇 With the `#[should_panic]` annotation we can assert that we expect the code
|
||||
// under test to panic. We can also check the panic message by using `expected`.
|
||||
// This is all part of Rust's built-in test framework!
|
||||
#[should_panic(expected = "The journey took no time at all. That's impossible!")]
|
||||
fn by_zero() {
|
||||
speed(0, 10, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user