4 ms·
Rust doesn't have stack allocated strings, I have no idea what you're talking about. We have string slices, but there's no way to stack allocate those, they do
by cmrx64 13y ago
Rust doesn't have stack allocated strings, I have no idea what you're talking about.
We have string slices, but there's no way to stack allocate those, they don't own their allocation.
- MichaelGG 13y agoI must have misread then. I thought in Rust there were various pointer types denoting ownership, and one of them was callstack-scoped (borrowed pointer?), meaning it could be backed by stack allocated memory.
- cmrx64 13y agoWell, yes, borrowed pointers can point onto the stack, but we don't have string values that can live on the stack, not really. String slices, denoted as `&str`, can point into the stack if you construct them from a `[u8, ..N]` (a fixed-length vector). String literals are stored in the ro section, as one might expect.
- MichaelGG 13y agoRight, literals, but for example, suppose I'm creating a ton of temporary strings (e.g. appending a filename to get a full path and calling touch on it) - in C#, I'm going to heap-alloc a new string for each call. With Rust, I _could_ compose it into a stack-alloc'd buffer, and pass it any function taking a string it doesn't need to own, correct?
- dbaupp 13y agoYes, you can put a fixed-length buffer of bytes (e.g. `[u8, .. 1024]` is 1kb) on the stack and use that as the backing vector for the string.