Development

Strings in Rust Explained

In this article, we will explore how to work with Strings in the Rust programming language and determine what makes string in Rust different.
Captain Salem 2 min read
Strings in Rust Explained

A string is defined as a sequence of Unicode characters that are encoded into 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.

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 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 does infer 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 do 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("cloudenv");

To heap allocate a string, we can do:

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

To convert a string literal to a string object:

let str = "cloudenv".to_string();

String Indexing

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

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

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

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

The code above should return 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 = "cloudenv";
  println!("{}", &string[0..5]);
}

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

cloud

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 to a &str.

Conclusion

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

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.