In Rust, can you own a string literal?

后端 未结 1 1509
清歌不尽
清歌不尽 2020-12-06 19:12

According to The Rust book:

Each value in Rust has a variable that’s called its owner. There can be only one owner at a time. When the owner goes out

相关标签:
1条回答
  • 2020-12-06 19:50

    A string slice reference (&str) does not own the string slice that it points to, it borrows it. You can have several immutable references to an object, that's why your second code sample is correct and the borrow checker is happy to accept it.

    I think you can say that types with the 'static lifetime have no owner, or that something outside of the main function owns it. The owner only matters when the lifetime of the owning object ends (if you own it, you need to free resources). For references only lifetimes matter.

    0 讨论(0)
提交回复
热议问题