educative.io

String Literals

In the intro of strings it was stated that a string literal is a slice of strings, then why is it possible to create a slice of a string literal

Hi @Olumuyiwa_Ajiboye,
In Rust, string literals are actually string slices themselves. When you create a string literal, like "Hello, World!", it is stored in a specific location in memory, and it has a fixed size known at compile time. This is why you can create a slice of a string literal in Rust.

This lesson discusses slicing a string, it’s showing you how to create a new string slice from a string literal or an existing String type. You can create slices from both because, conceptually, a string literal is a reference to a fixed-size string slice.

So, while the terminology may seem a bit confusing, you can indeed create a slice of a string literal in Rust because string literals themselves are treated as slices of characters. This allows you to work with string literals in a similar way to other string types in Rust.

I hope this helps!

Regards!
Komal Arif
Sr. Developer Advocate at Educative