How to extract the end of a string (suffix) in Rust

1 Answer

0 votes
fn get_suffix(input_str: &str, suffix_length: usize) -> &str {
    let length_of_suffix = std::cmp::min(suffix_length, input_str.len());
    let start_index = input_str.len() - length_of_suffix;
    
    &input_str[start_index..]
}

fn main() {
    let input_str = "Software Programmer";
    let suffix_length = 5; // Change this value to get a different suffix length

    let suffix = get_suffix(input_str, suffix_length);

    println!("The suffix is: {}", suffix);
}



/*
run:

The suffix is: ammer

*/

 



answered May 1 by avibootz
...