local_strtools 0.1.1

Collection of string related utilities
Documentation
//! # String tools
//!
//! `strtools` is a collection of string related utilities, not provided by standart
//! library. Especially useful when your project deals with lot of manipulations
//! around `String` or `&str`

/// Pads a string with zeros, resulted string would be in length given in second argument
///
/// # Examples
/// ```
/// let str = String::from("9");
/// let padded_string = strtools::pad(str, 3);
/// 
/// assert_eq!(padded_string, "009");
/// ```
/// # Panics
/// When length of given string is bigger than the wanted to be length, program panics:
/// ```
/// #[should_panic]
/// fn can_panic() {
///    let str = String::from("98798");
///    strtools::pad(str, 3);
/// }
/// ```
///
pub fn pad(str: String, l: usize) -> String {
    let len = str.len();
    if len > l {
        panic!("Cannot pad string that is bigger than needed to be");
    }

    let padded = l - len;
    let mut zeros: Vec<&str> = vec![];

    for _ in 1..=padded {
        zeros.push("0");
    }

    format!("{}{}", zeros.join(""), str)
}