Rust Lang

String to Str Conversion in Rust

In Rust, string and str are two distinct types which are commonly used to work with string or text data.

A string is a dynamically sized, mutable type that represents a heap-allocated string of UTF-8 encoded characters. It is applied as a growable buffer of bytes which are resizeable to accommodate the string’s content.

On the other hand, str is a statically sized, immutable type that represents a sequence of UTF-8 encoded characters. It is implemented as a slice of bytes which means that it points to a contiguous region of memory that contains the characters of the string.

The main difference is their mutability. For example, a string type can be modified after creation in Rust, whereas an str type cannot.

This is because the string owns the memory that it occupies and can allocate and deallocate a memory as needed to modify its contents. On the other hand, an str borrows its memory from another source (usually a string or a string literal) and is therefore bound to the immutability of that source.

Sometimes, you may encounter a scenario where you must convert a string type to an str in Rust. This tutorial walks you through how to accomplish this using the built-in Rust features.

Convert the String to Str

The main question is can we directly convert a string to a str type in Rust? Unfortunately, the short answer is no.

Due to the fundamental difference in memory ownership and lifetime semantics between the two types, converting a string to an str type is not so direct.

The most common way of converting a string to a str type is by obtaining an “&str” reference to the contents of the string using the as_str() method. The method should return an immutable slice of the string contents.

An example is as follows:

let my_string = String::from("Hello, world!");
let my_str = my_string.as_str();

 

The previous code should return a reference to the contents of the string.

This allows us to use the string contents as an str without converting it to an str type.

NOTE: The “&str” reference from the as_str() method has the same lifetime as the string from which it was created. Hence, it is subject to the same memory management rules as the original string type.

Another method that we can use is simply creating a new str by taking a reference to a string literal or another str.

Example Code:

let my_str = "Hello, world!";
let my_string = String::from(my_str);
let my_other_str: &str = &my_string;

 

In the previous example, we start by creating a string literal. Then, we use it to create a new string using the from() method. We can then take a reference to the string’s contents using the “&” operator which gives us a reference to an str.

Conclusion

We explored the fundamental differences between the string and str types in Rust. We also covered the possibilities of converting a string to an str type.

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