Conceptual
Login

String vs &str

String is an owned, growable UTF-8 buffer (a Vec<u8> with a validity invariant); &str is a borrowed slice viewing UTF-8 bytes owned elsewhere, including string literals in the binary. APIs take &str to accept both, and String derefs to &str for free.

This Concept is waiting for its first lesson!

String is an owned, growable UTF-8 buffer (a Vec<u8> with a validity invariant); &str is a borrowed slice viewing UTF-8 bytes owned elsewhere, including string literals in the binary. APIs take &str to accept both, and String derefs to &str for free.

Are you a teacher? Sign in to start contributing.

Sign In