rufl 0.1.3

A powerful util function library for rust
Documentation
/// Removes the specified substring which first occurrence in the source string.
///
/// # Arguments
///
/// * `s` - The input string to perform remove.
/// * `substr` - The substring to be removed.
///
/// # Returns
///
/// Returns string after being removed.
///
/// # Examples
///
/// ```
/// use rufl::string;
///
/// let foo = string::remove_first("abca", "a");
/// assert_eq!("bca".to_string(), foo);
///
/// let bar = string::remove_first("abc", "d");
/// assert_eq!("abc".to_string(), bar);
///
///
/// ```

pub fn remove_first(s: impl AsRef<str>, substr: &str) -> String {
    match s.as_ref().find(substr) {
        Some(i) => s.as_ref()[0..i].to_string() + &s.as_ref()[i + substr.len()..].to_string(),
        None => s.as_ref().to_string(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_remove_first() {
        assert_eq!("abc".to_string(), remove_first("abc", ""));
        assert_eq!("bac".to_string(), remove_first("abac", "a"));
        assert_eq!("abc".to_string(), remove_first("abc", "d"));
    }
}