Replace space in string with U+00A0

Hello, I'm a Rust beginner. Suppose I have a string "hello world". My goal is to replace the space between "hello" and "world", from U+0020 to U+00A0 which is the "non-break space" unicode character. My code is something like this:

let mut teststring = "hello world";
for s in teststring.chars() {
if s == ' ' {
//replace s with U+00A0

I can't find any method to encode U+00A0 and replace it in teststring. Can someone help?

Modifying the original string in-place can be a bit tricky and/or inefficient. Rust strings are stored in UTF-8, so U+0020 takes only one byte, while U+00A0 takes two bytes. So for each space you replace, you would also need to move all of the following characters over to make room for the extra byte. Also, if you do this while iterating over the string, it would invalidate the iterator since the next character's location has changed (and also because the string has grown, which could require moving it into a new allocation).

Instead, it can be simpler to create a new string. You could do this by looping over the characters, as in your original code:

let teststring = "hello world";
let mut new_string = String::new();
for char in teststring.chars() {
    if char == ' ' {
    } else {

or you can use the built-in replace method:

let teststring = "hello world";
let new_string = teststring.replace(" ", "\u{a0}");

Thank you!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.