1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
use rand::Rng; use std::str; /// Returns a string with random case /// /// # Arguments /// /// * `x` - A string to be modified /// /// # Examples /// /// ``` /// // You can have rust code between fences inside the comments /// // If you pass --test to `rustdoc`, it will even test it for you! /// use loser_case; /// loser_case::to_loser("test"); /// ``` pub fn to_loser(x: &str) -> String { let mut rng = rand::thread_rng(); let vector = x .chars() .map(|a| { if rng.gen_range(0, 10) > 5 { a.to_ascii_uppercase() } else { a.to_ascii_lowercase() } }) .collect::<String>(); vector } #[cfg(test)] mod tests { use super::*; #[test] fn it_works() { assert_eq!("test", to_loser("test").to_lowercase()); } }