Equivalent of C++ std::string.erase()

⚓ Rust    📅 2025-09-10    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 64 days ago. The information described in this article may have changed.

Suppose we have a text string and we've found the character position of a word, after a delimiter such as a space, as well as the character length of the word, i.e., to the next delimiter, and we wanted to remove that word. In C++, given text, beg and len as the given variables, we could do that with:

text.erase(beg, len);

Looking at Rust's std::string, I came up with the following "equivalent" code:

text.replace_range(beg..(beg + len), "");

However, it looks "hacky" to me. Is there a better approach using just Rust and its std library?

5 posts - 3 participants

Read full topic

🏷️ Rust_feed