imstr icon indicating copy to clipboard operation
imstr copied to clipboard

What is the difference for the ordinary string and &str ?

Open Kiddinglife opened this issue 2 years ago • 1 comments

Hey, seems like all the features in this library can be achieved via the use of rust string and &str slice. So anything I missed? Thx.

Kiddinglife avatar Apr 13 '23 21:04 Kiddinglife

Hey, sorry I only saw your issue right now.

There are two main differences:

  • When you clone() a String, you have to allocate new memory and copy the contents of the string over. Whereas when you clone() an ImString, you're just incrementing a reference count, but not duplicating the data. The data inside an ImString can be cheaply shared.
  • The difference between an ImString and a &str is that you cannot easily store a &str in, say, a struct or a hashmap and pass it around. It has a lifetime attached to it. Since you cannot have self-referential structs, you cannot have, say a struct that contains a String and a bunch of string slices (&str) pointing to it. That is what this crate solves: you can use an ImString like a &str, but it does not need a lifetime.

I hope that answers your questions.

xfbs avatar Apr 15 '23 20:04 xfbs