Summary

  • Everything we use in our program must be stored in memory

  • The stack is the primary place where our values are stored

  • The stack cannot hold Dynamically Sized Types

  • Strings are Dynamically Sized Types, and therefore we store references to them on the stack

  • String literals are stored in program memory

  • References to string literals have a special reference lifetime, 'static

  • You can heap allocate memory for strings

  • The String struct wraps up a heap allocation in a type that can be stored on the stack

  • The String struct is also known as an owned string, while str is a string slice

  • You can use the + operator to combine owned strings

  • The long name for String is std::string::String, which you’ll sometimes see in error messages

  • Thanks to deref coercion, when you borrow a String, you can get either a &String or a &str

  • When you write functions, it’s better to use &str rather than &String as parameters so that your functions work on both owned strings and string literals

  • It’s typically best practice to store owned Strings in structs rather than references

  • You can use the format! macro to construct Strings

  • Owned Strings automatically handle freeing of memory, and prevent a lot of very common bugs

Get hands-on with 1400+ tech skills courses.