9951 explained code solutions for 126 technologies


rustrust string api


Rust strings are a powerful and efficient way to store and manipulate text data. They are stored as UTF-8 encoded data, which allows for efficient storage and manipulation of text data. Rust strings are immutable, meaning that once created, they cannot be changed.

Example code

let mut s = String::from("Hello");
s.push_str(", world!");
println!("{}", s);

Output example

Hello, world!

Code explanation

  • let mut s = String::from("Hello");: This line creates a mutable string variable s and assigns it the value Hello.
  • s.push_str(", world!");: This line adds the string ", world!" to the end of the string s.
  • println!("{}", s);: This line prints the value of the string s to the console.

Helpful links

Edit this code on GitHub