Rust String

Rust devided string in two types: &str and String.

Rust String(String Object)

A string is encoded in a UTF-8 sequence. String object type is provided in Standard Library. A string is growable in size.It represent string values that are provided at runtime.

Rust &str(String literal)

&str is also known as a string slice. &str are set of characters, which are hardcoded into a variable.String literals are static by default.String literals are static duration of entire program.

Rust &str Example

fn main() {
   let s:&str = "Hello World";
   println!(" {} ",s);


Rust Methods

Get method Post method
new() Creates a new empty String.
to_string() Converts the given value to a String.
replace() Replaces all matches of a pattern with another string.
as_str() Extracts a string slice containing the entire string.
push() Appends the given char to the end of this String.
push_str() Appends a given string slice onto the end of this String.
len() Returns the length of this String, in bytes.
trim() Returns a string slice with leading and trailing whitespace removed.
split_whitespace() Splits a string slice by whitespace and returns an iterator.
split() Returns an iterator over substrings of this string slice, separated by characters matched by a pattern.
chars() Returns an iterator over the chars of a string slice.

© copyright 2017-2022 Completedone pvt ltd.