This post provides guidelines for choosing between `String` and `&str` in Rust. It introduces four levels of recommendations: using `String` by default, preferring `&str` for function parameters, sometimes returning `&str`, and considering `&str` in structs only when necessary. These rules help simplify code and manage string ownership and lifetime effectively.

7m read timeFrom steveklabnik.com
Post cover image
Table of contents
Oct 06 2024Level 1: Don’t think about it at allLevel 2: prefer &str for function parametersLevel 3: return &str sometimesLevel 4: When to use &str in a struct

Sort: