Unsized types

Unsized types are categories of types that are first encountered if one tries to create a variable of the type, str. We know that we can create and use string references only behind references such as &str. Let's see what error message we get if we try to create a str type:

// unsized_types.rsfn main() {    let a: str = "2048";}

We get the following error upon compilation:

By default, Rust creates a reference type of str as 'static str. The error message mentions that all local variables—values that live on the stack—must have a statically known size at compile time. This is because the stack memory is finite and we cannot have ...

Get Mastering Rust - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.