Files
100-exercises-to-learn-rust/exercises/04_traits/01_trait/src/lib.rs
Fangyi Zhou 5bb9333ae9 Add i32/u32 suffix for numeric literals in 04_traits/01_trait (#20)
Without an explicit suffix, the compiler is able to use the i32
implementation without the need for an u32 implementation.
2024-05-22 11:04:04 +02:00

24 lines
524 B
Rust

// Define a trait named `IsEven` that has a method `is_even` that returns a `true` if `self` is
// even, otherwise `false`.
//
// Then implement the trait for `u32` and `i32`.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_u32_is_even() {
assert!(42u32.is_even());
assert!(!43u32.is_even());
}
#[test]
fn test_i32_is_even() {
assert!(42i32.is_even());
assert!(!43i32.is_even());
assert!(0i32.is_even());
assert!(!(-1i32).is_even());
}
}