String in Rust

12 August, 2020   ·   Rust

in the core language, Rust provides only one type of string: str, which is often seen in its borrowed form &str.

Rust’s standard library provdes another type: String. this type is growable, owned and mutable.

creating new String

let s1 = "some string".to_string(); // create string from literals
let mut s = String::new(); // creates an empty string
let s2 = String::from("initial content"); // initialize with a value

// strings are utf-8 encoded. it can include any
// properly encoded data
let hello = String::from("नमस्ते");

updating string

// using .push_str() method
let mut s = String::from("foo");

// push a char using .push() method

// concate with + and format!() macro
let s1 = String::from("Hello, ");
let s2 = String::from("world!");
let s3 = s1 + &s2; 
// note s1 has been moved and can no longer be used

format!("{}{}", s1, s2);

Bytes, Scalar values and Grapheme Clusters

example string: “नमस्ते”

accessing elements in a string

String values can be accessed with a for loop.

// print elements as char
for c in "नमस्ते".chars() {
  print!("{} ", c);
// prints: न म स ् त  े

// print elements as bytes
for b in "नमस्ते".bytes() {
  print!("{} ", b);
// prints: 224 164 ...... 165 135

Rust’s standard library does not implement functionality for getting grapheme clusters. external libraries does.

 string  string  collection
Vector in Rust↠ ↞Regular Expression