Evan Carroll
Evan Carroll

Reputation: 1

Why does the &str primitive exist?

If String is actually

pub struct String {
    vec: Vec<u8>,
}

Then why is there a special syntax (&str) for a slice of a Vec<u8>? In Chapter 3 of "Programming Rust" by Jim Blandy & Jason Orendorff it says,

&str is very much like &[T]: a fat pointer to some data. String is analogous to Vec<T>

Following that statement there is a chart which shows all the ways they're similar, but there isn't any mention of a single method that they're different. Is a &str; just a &[T]?

Likewise in the answer to, What are the differences between Rust's String and str? it says

This is identical to the relationship between a vector Vec<T> and a slice &[T], and is similar to the relationship between by-value T and by-reference &T for general types.

That question focuses on the difference between String and &str. Knowing that a String really is a vector of u8, I'm more interested in &str, which I can't even find the source to. Why does this primitive even exist when we have a primitive (implemented as a fat pointer) for regular vector slices?

Upvotes: 3

Views: 382

Answers (2)

Shepmaster
Shepmaster

Reputation: 430831

It exists for the same reason that String exists, and we don't just pass around Vec<u8> for every string.

  • A String is an owned, growable container of data that is guaranteed to be UTF-8.
  • &str is a borrowed, fixed-length container of data that is guaranteed to be UTF-8
  • A Vec<u8> is an owned, growable container of u8.
  • &[u8] is a borrowed, fixed-length container of u8.

This is effectively the reason that types exist, period — to provide abstraction and guarantees (a.k.a. restrictions) on a looser blob of bits.

If we had access to the string as &mut [u8], then we could trivially ruin the UTF-8 guarantee, which is why all such methods are marked as unsafe. Even with an immutable &[u8], we wouldn't be able to make assumptions (a.k.a. optimizations) about the data and would have to write much more defensive code everywhere.

but there isn't any mention of a single method that they're different

Looking at the documentation for str and slice quickly shows a number of methods that exist on one that don't exist on the other, so I don't understand your statement. split_last is the first one that caught my eye, for example.

Upvotes: 9

Boiethios
Boiethios

Reputation: 42759

&str is not necessarily a view to a String, it can be a view to anything that is a valid UTF-8 string.

For example, the crate arraystring allows creating a string on the stack that can be viewed as a &str.

Upvotes: 7

Related Questions