How to split a string in half but not in the middle of a word with Rust

1 Answer

0 votes
fn main() {
    let str = "c# c java c++ python go rust";
    
    let halfstr = &str[0..(str.len() / 2) + 1];
    let center = halfstr.rfind(' ').unwrap() + 1;

    let parts = [&str[0..center], &str[center..]];

    println!("{}", parts[0]);
    println!("{}", parts[1]);
}


  
/*
run:

c# c java c++ 
python go rust

*/

 



answered Sep 10, 2024 by avibootz
...