What are the differences between Rust's `String` and `str`?

前端 未结 9 818
醉梦人生
醉梦人生 2020-11-22 05:11

Why does Rust have String and str? What are the differences between String and str? When does one use String

9条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 06:07

    For C# and Java people:

    • Rust' String === StringBuilder
    • Rust's &str === (immutable) string

    I like to think of a &str as a view on a string, like an interned string in Java / C# where you can't change it, only create a new one.

提交回复
热议问题