O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Takeaways and tasks

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:

  1. Create a few string slices and Strings, and print them. Use both push and push_str to populate a String with data.
  2. Write a function that takes a string slice and prints it. Pass it a few static string slices and a few Strings.
  3. Define byte strings with both UTF-8 strings and non-UTF-8 strings. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required