Daily Learnings: Mon, Apr 08, 2024
I will love the light for it shows me the way, yet I will endure the darkness because it shows me the stars. — Og Mandino
Notes on Rust Learnings - String Types
It’s been a really long time, but I finally got back into the massive and detailed rust YouTube course by freeCodeCamp, and spent some time learning about the different types of strings in the rust programming language.
- There are 3 “string” types to consider in rust:
String&str- String literals
- Consider the String types as a “compound type”, as they’re made up of
chartypes String- Stored in the heap
- Owns its contents
- Is mutable
&str- Called “string slice”
- Stored as UTF-8 bytes in-memory
- Does not own its underlying data
- It’s more of a “view” of a sequence of characters
- Immutable
- Considered more lightweight and efficient than
String
- String literals
- Sequence of characters hard-coded between double-quotes (
") - They are fixed-size, known at compile-time, and stored as a sequence of UTF-8 bytes
- Type definition:
&' static str- In practice, in your programs, it’s actually typed as
&str - This indicates that the data is stored in static storage
- The data is hard-coded into the final executable and stored in “read-only memory”
- Valid through the lifetime of the program
- In practice, in your programs, it’s actually typed as
- Immutable
- Sequence of characters hard-coded between double-quotes (