Rust Lang

How to Use Strings in Rust

A string is defined as a sequence of Unicode characters that are encoded into the UTF-8-byte stream. Strings are a 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 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 using the push_str method.


To heap allocate a string, we can do the following:

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

To convert a string literal to a string object, we can do the following:

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. Then, we can iterate over the slices as shown:

let string = "Linuxhint";
for c in string.as_bytes() {
  println!("{}", c);

The previous code 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 below:

fn main() {
  let string = "Linuxhint";
  println!("{}", &string[0..5]);

The previous example should return the string available at the specified offsets:


String Concatenation

You can concatenate a string literal and a string object using the + operator. The following example is provided below:

fn main() {
  let str1:&str = "Hello";
  let str2:&str = " world";
  let string = str1.to_string() + str2;
  println!("{}", string);

Note: 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 follows:

fn main() {
  let str1 = "Hello".to_string();
  let str2 = " world".to_string();
  let string = str1.to_string() + &str2;
  println!("{}", string);

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


In this guide, we explored the concept of strings in the Rust programming language and discussed how we could use them in our programs. We highlighted the two different types of strings that are the String Literals &str and the String Objects. In addition, we discussed the different methods to heap allocate a string. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

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