9951 explained code solutions for 126 technologies


rustrust string and str


A String in Rust is a type of data structure that stores a collection of characters. It is a heap-allocated data structure and is growable, meaning it can be appended to. str is a type of string literal that is statically allocated and is not growable.

Example code

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

let s2 = "Hello, world!";

Output example

Hello, world!

Code explanation

  • let mut s = String::from("Hello");: This line creates a mutable String called s and assigns it the value "Hello".
  • s.push_str(", world!");: This line appends the string ", world!" to the end of s.
  • let s2 = "Hello, world!";: This line creates a str called s2 and assigns it the value "Hello, world!".

Helpful links

Edit this code on GitHub