Rust Strings

Pushing an entire String

let mut hi = String::from("Hey there...");
hi.push\_str("How are you doing??");
// => Hey there...How are you doing??
println!("{hi}");

Pushing a single character

let mut half\_text = String::from("Hal");
half_text.push('f');    // => Half

.contains()

let name = String::from("ElementalX");
name.contains("Element") // => true

Checks if the substring is contained inside the original string or not.

.capacity()

let rand = String::from("Random String");
rand.capacity()  // => 13

Calculates the capacity of the string in bytes.

String Object

// Creating an empty string object
let my\_string = String::new;
// Converting to a string object
let S\_string = a_string.to\_string()
// Creating an initialized string object
let lang = String::from("Rust");  
println!("First language is {lang}");
Comments