How to extract the beginning of a string (prefix) in Rust

1 Answer

0 votes
fn extract_prefix(s: &str, length_of_prefix: usize) -> &str {
    // Extract the prefix
    &s[..length_of_prefix]
}

fn main() {
    let s = "Software Programmer";
    let length_of_prefix = 4; // Length of the prefix you want to extract

    let prefix = extract_prefix(s, length_of_prefix);

    println!("Prefix: {}", prefix);
}



/*
run:

Prefix: Soft

*/

 



answered May 1 by avibootz
...