Updating a String

This lesson will teach to update a string in Rust.

An existing string can be updated by appending a character or a string.

💡 Why not make a new String rather than updating an existing one?

Updating an existing String is useful when you want to make changes to an existing String at run time rather than compile one like, in situations where changes are made to the String on a condition.

Push a Single Character

There are cases when it is required to update a string by pushing a single character. One example is to create a string which contains a single character repeated N times on a particular condition. Rust helps you do it by using the push method.

Steps to push a character to a String:

  • Make a mutable string variable.

  • To push a single Unicode character to a String object, pass a character within the push() built-in method.

    The following code shows how to do it!

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy