3 ms·
`String` copies are always `String`s and will live on the heap. AFAIK there are no stack strings (plain `str` which I guess is what you mean?) IIRC small fixed
by kaoD 9y ago
`String` copies are always `String`s and will live on the heap. AFAIK there are no stack strings (plain `str` which I guess is what you mean?)
IIRC small fixed-size byte arrays (`[N; u8]`) are sometimes allocated on the stack depending on their size, but they are plain bytes and not full-fledged UTF8 strings. You can convert them into `String` but that would heap-allocate them.
To expand on this: in Rust copy strinctly means there is a new owner that is tasked with deallocating that data once it goes out of scope. Move is the same but the ownership is transferred (thus the old owner is no longer responsible for deallocating anything) insted of having a new copy and an additional owner. Copy always results in a new object of the same type.
& types are always borrowing. You can't copy into a reference since references are just borrowing of data, and owners of references won't deallocate anything (since they assume that, as long as they can hold an &, the data they reference is still alive, which is true because owners can't deallocate anything if there is a borrow in place).
EDIT: I was wrong. As sibling comment says, you can convert a stack-allocated fixed-size array slice into `&str` with `str::from_utf8`.
Check the last example here: https://doc.rust-lang.org/std/str/fn.from_utf8.html#examples https://doc.rust-lang.org/std/str/fn.from_utf8.html#examples