pub mod impl_dyn;
pub mod closure;
pub mod crates;
#[allow(dead_code)]
pub fn add(left: usize, right: usize) -> usize {
left + right
}
#[allow(dead_code)]
#[derive(Debug)]
struct Rectangle {
height: u32,
width: u32,
}
impl Rectangle {
#[allow(dead_code)]
fn new(height: u32, width: u32) -> Self {
if height < 100 && width < 100 {
Self { height, width }
} else {
panic!("Oversized rectangle!");
}
}
#[allow(dead_code)]
fn can_hold(&self, another: &Rectangle) -> bool {
self.height > another.height && self.width > another.width
}
}
pub fn external_adder(a: i32, b: i32) {
internal_adder(a, b);
}
fn internal_adder(x: i32, y: i32) -> i32 {
x + y
}
#[cfg(test)]
#[allow(dead_code, unused_variables)]
mod tests {
use crate::closure::{Inventory, ShirtColor};
use super::*;
#[test]
fn exploration() {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
fn can_hold_smaller() {
let rect1 = Rectangle {
height: 7,
width: 8,
};
let rect2 = Rectangle {
height: 3,
width: 2,
};
assert_eq!(rect1.can_hold(&rect2), true); assert_ne!(rect2.can_hold(&rect1), true) }
#[test]
#[should_panic(expected = "Oversized rectangle!")]
fn should_panic() {
let rect3 = Rectangle::new(1000, 500);
}
#[test]
fn using_enum() -> Result<(), String> {
if 2 + 2 == 4 {
Ok(())
} else {
Err(String::from("2 + 2 != 4"))
}
}
#[test]
fn testing_private_func() {
assert_eq!(10, internal_adder(3, 7));
}
#[test]
fn testing_closure() {
let user_pref1 = Some(ShirtColor::Blue);
let user_pref2 = None;
println!("user pref1: {:?}", user_pref1);
println!("user pref2: {:?}", user_pref2);
let inventory = Inventory {
shirts: vec![
ShirtColor::Blue,
ShirtColor::Red,
ShirtColor::Blue,
ShirtColor::Red,
ShirtColor::Red,
ShirtColor::Red,
],
};
let giveaway1 = inventory.giveaway(user_pref1);
let giveaway2 = inventory.giveaway(user_pref2);
println!("user1 received: {:?}", giveaway1);
println!("user2 received: {:?}", giveaway2);
assert_eq!(ShirtColor::Blue, giveaway1);
assert_eq!(ShirtColor::Red, giveaway2);
}
}