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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
use std::io::stdin; /// Gets the first char of a string, and returns it as string. /// /// ``` /// let hello = String::from("Hello"); /// let first = yarns::head(&hello); /// //first = (String)"H" /// ``` pub fn head(word: &String) -> String { word.chars().take(1).collect() } /// Returns a new string starting at the index provided in the second argument, going the length provided as the third argument. /// /// ``` /// let hello = String::from("Hello"); /// let sub= yarns::substr(&hello, 1, 3); /// //sub = (String) "ell" /// ``` pub fn substr(word: &String, start: usize, length: usize) -> String { let res: String = word.chars().skip(start).take(length).collect(); res } /// Concatenates two strings. /// /// ``` /// let hello = String::from("Hello"); /// let world = String::from("World"); /// let hw = yarns::concat(&hello, &world); /// //hw = (String)"HelloWorld" /// ``` pub fn concat(str1: &String, str2: &String) -> String { format!("{}{}", str1, str2) } /// Returns wether a given String contains the given Substring. /// /// ``` /// let hello = String::from("Hello"); /// let ell = String::from("ell"); /// let has_it = yarns::contains(&hello, &ell); /// //has_it = (bool) true /// ``` pub fn contains(main: &String, sub: &String) -> bool { main[..].contains(&sub[..]) } /// Shortcut for simple binding of user input to an immutable String. pub fn read() -> String { let mut temp = String::new(); stdin().read_line(&mut temp).unwrap(); temp.trim().to_string() } /// Returns the length of the utf-8 representation of a string. /// /// ``` /// let hello = String::from("Hello"); /// let length = yarns::len(&hello); /// //length = 5 /// ``` pub fn len(string: &String) -> usize { string.chars().count() }