April 2026
Intermediate
631 pages
16h 20m
English
Rust provides a feature called unsized coercion, which allows a sized type to be transformed into an unsized type. This mechanism is somewhat similar to deref coercion, which we covered in Chapter 10, Section 10.3. Let’s first revisit deref coercion, and then we’ll dive into how unsized coercion differs.
Deref coercion enables the automatic conversion of a reference from one type to another, particularly when interacting with methods or functions expecting a certain type. Consider the following function that accepts a string slice as an input:
fn str_slice_fn(s: &str) {}
You can call this function in main with inputs that can be dereferenced into a String slice, as shown in Listing 13.19.
Read now
Unlock full access