News for hackers. Stuff that matters.
Re:Rust Language Cheat Sheet by pitaj on Sunday April 25, 2021 @10:38AM (#26933037)
If you use String which is heap allocated, you don't really need to worry about lifetimes.

&str on the other hand

Re:Rust Language Cheat Sheet by KingOfCoders on Sunday April 25, 2021 @11:03AM (#26933267)
Yes, String was an oversimplification, String means I either own or copy it, both things I usually don't want to do, so I end up - as you've said - with &str.
Re:Rust Language Cheat Sheet by phaylon on Sunday April 25, 2021 @11:19AM (#26933391)
You might want to look into using `Arc<str>`. It's an immutable string slice that can have many owners and will be dropped once the last one is done with it.
Re:Rust Language Cheat Sheet by KingOfCoders on Sunday April 25, 2021 @01:03PM (#26934315)
Thanks, I will try that - fearing my code is littered with Arc ;-) - but sarcasm aside, thanks.
Re:Rust Language Cheat Sheet by phaylon on Sunday April 25, 2021 @01:12PM (#26934397)
I usually just have a `type Text = std::sync::Arc<str>` at the root, specifically during prototyping :)
Re:Rust Language Cheat Sheet by rmdashrfstar on Sunday April 25, 2021 @01:10PM (#26934376)
How does this differ from just sharing &str?
Re:Rust Language Cheat Sheet by phaylon on Sunday April 25, 2021 @01:14PM (#26934427)
A `&str` is a borrow of the string slice data, and has borrowing semantics. An `std::sync::Arc<str>` on the other hand isn't borrowed but has shared ownership and is atomically reference counted.

Does that answer the question? Could you expand on what you mean by sharing `&str`?

Re:Rust Language Cheat Sheet by rmdashrfstar on Sunday April 25, 2021 @07:33PM (#26937751)
I suppose my question is what motivates the use of atomically referencing counting string slice rather than borrowing?
Re:Rust Language Cheat Sheet by phaylon on Sunday April 25, 2021 @10:01PM (#26938517)
You can store it independently without it being borrowed. So your structs don't need lifetimes.

I consider it a midpoint between `String` and `&str`. Most of the convenience of `String` (barring mutation) but less costly.