Rust Lang

Strings in Rust

A string is defined as a sequence of Unicode characters that are encoded into a UTF-8-byte stream. Strings are a very fundamental concept in any programming knowledge as they are a source of great trouble or great productivity.

This article will explore how to work with Strings in the Rust programming language and determine what makes strings in Rust different.

Rust Strings

There are two types of strings in Rust:

  1. String Literals &str
  2. String Objects (String)

Rust String Literal

The Rust string literal is known as a string slice that always references a sequence of UTF-8 characters. We mainly use it when we know the value of the string at compile time. It can be used as a view into a string object. String literals are static by default, meaning they do not mutate.

We can declare a string literal in Rust by creating a variable with or without type inference. The following example shows two valid string literals in Rust.

let str = "Hello world";

let str:&str = "Hello world;

Both are similar, but one infers the type while the other does not.

Rust String Object

A string object refers to a heap-allocated and dynamic vector of bytes. Like string literals, String objected does not need to be null-terminated. String objected are typically created by converting a string slice to string objects using the to_string method.

To create an empty and growable String object, we can use the new method as shown:

let mut str = String::new();

Once you have an empty string, you can add a value to it using the push_str method.

str.push_str("Linuxhint");

To heap allocate a string, we can do:

let name = String::from("Winnie");

To convert a string literal to a string object:

let str = "Linuxhint".to_string();

String Indexing

Strings in Rust do not support direct indexing. An example is as shown:

let string = "Linuxhint";

println!("{}", string[0]);

The best way to overcome this is to treat the string as a sequence of individual bytes. We can then iterate over the slices as shown:

let string = "Linuxhint";

for c in string.as_bytes() {

println!("{}", c);

}

The code above should return an individual byte representation of the string values.

String Slicing

You can get a slice of a string using the slicing literal as shown:

fn main() {

let string = "Linuxhint";

println!("{}", &string[0..5]);

}

The above should return the string available at the specified offsets.

Linux

String Concatenation

You can concatenate a string literal and a string object using the + operator. An example is as shown:

fn main() {

let str1:&str = "Hello";

let str2:&str = " world";

let string = str1.to_string() + str2;

println!("{}", string);

}

Keep in mind you cannot concatenate two &str values using the + operator.

If you have two string objects, you can concatenate both of them by using the & operator as:

fn main() {

let str1 = "Hello".to_string();

let str2 = " world".to_string();

let string = str1.to_string() + &str2;

println!("{}", string);

}

This is because of Deref coercion that allows a String to coerce a &str.

Conclusion

In this guide, we explored the concept of strings in Rust and discussed how we could use them in our programs.

Thanks for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list