How to convert array of chars to string in Rust

1 Answer

0 votes
fn char_array_to_string(char_array: &[char]) -> String {
    // Use .iter() and collect to convert the char array to a String
    char_array.iter().collect()
}

fn main() {
    let char_array = ['H', 'e', 'l', 'l', 'o'];
    
    // Convert the char array to a String
    let result_string = char_array_to_string(&char_array);
    
    println!("Resulting string: {}", result_string);
}



      
/*
run:

Resulting string: Hello
     
*/

 



answered Apr 5, 2025 by avibootz
...