Let's wrap up strings. Here's what to remember:
- There are two string types: String and &str
- Strings in Rust are guaranteed to be Unicode
- When passing strings to functions, favor the &str type
- When returning strings from functions, favor the String type
- Raw byte strings are arrays or vectors of 8-bit unsigned integers (u8)
- Strings are heap-allocated and dynamically grown, which makes them flexible but costlier
Here are a few tasks:
- Create a few string slices and Strings, and print them. Use both push and push_str to populate a String with data.
- Write a function that takes a string slice and prints it. Pass it a few static string slices and a few Strings.
- Define byte strings with both UTF-8 strings and non-UTF-8 strings. ...