Rust Strings, the complete overview

ยท

4 min read

Everything in Rust Programming Language could look like an overdo, or unnecessarily complex, except that they are only a myth about the language.

In all sincerity, most things in real life are exactly not black and white, but - in between - gray. Strings aren't an exception.

What are strings?

Strings are a sequence of characters - 'H' 'E' 'L' 'L' 'O'.

A string is made up of two or more characters wrapped by double quotes. For example, '5' is a character and 'P' is another character that when combined turns into a string - "5P".

๐Ÿ’€ Before you read on, is this content right for you?

Audiences: This writing is intended for devs of all levels who have some idea of how to write a simple Rust program.

By the end of your reading, you will have gone through a friendly overview of strings in Rust and their types.

Forms of strings in Rust

Here are the forms of strings in Rust

  • character - e.g 'a'

  • string slice - e.g "able"

  • string - e.g "able" + "god" = "ablegod"

char Type

The char type represents a single character of the alphabet, number, punctuation or space wrapped in a single quote. For example,

  • "God " is made up of 4 characters - 'G', 'o', 'd' and ' '.

  • "Hey" is represented by 3 characters 'H', 'e' and 'y'.

Characters in Rust must be wrapped with a single quote. Anything wrapped in double quotes is treated as a string literal even if it is a character.

See the screenshot below about the Rust compiler complaining about "e" even though it is a single character. This is because it is wrapped in double quotes instead of single quotes.

The primitive string, String Slice

This is the closest to a typical string that comes to mind outside of Rust. This is due to the number of built-in methods it has at its disposal to perform on itself eg .contains(), .find(), .ends_with() etc.

String slices are read-only - immutable. You can't mutate them. It is denoted as str. Usually, it is accompanied by an amper & signs like this &str. This is because it is unlikely that it is used outside its borrowed form.

let _string: &str;

A string slice would always point to a byte or something in memory. This means they are supposed to have a lifetime (if this paragraph sounds confusing do not bother, for now, keep reading).

A string literal is a form of string slice. They are constants referenced directly from your source code.

String Slice with Static lifetime

let _str: &'static str = "Hello World"

After they are entered (or written), you do not try to change them, reduce or add new characters to them.

Growable String

Growable strings are identified with the type keyword, String. This behaves like the constructor type for strings. This is the mutable string type.

Popular utility string mutation like Concatenation - merging two string literals to become one - can only be done with this form of string.

// won't work, "Hello " is a literal and not growable string

let newString = "Hello " + "world" 
// literal + literal;
//๐Ÿ™Œ works because the first argument is a Growable string

let newString = String::from("Hello ") + "world" 
// String + literal

let newString = "Hello ".toString() + "w" 
// String + character

Conclusion

Thank you again for reading thus far. You need to know how I am filled with joy when someone finds my writing useful. My goal is to keep sharing as I learn more about Rust in such a simple and friendly manner.

Drop a like if you find this writing useful. It is one of the ways to let me know how great I am doing.

Let's connect on Twitter at kelvinsekx.


resources

[-] What's the difference between a string and a literal? https://stackoverflow.com/questions/7356135/whats-the-difference-between-a-string-and-a-literal

[-] A more in-depth exploration of Strings in Rust. https://betterprogramming.pub/strings-in-rust-28c08a2d3130

ย