Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Kiddinglife opened this issue Apr 13, 2023 · 1 comment
Open

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

Kiddinglife opened this issue Apr 13, 2023 · 1 comment

Comments

@Kiddinglife
Copy link

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.

@xfbs
Copy link
Owner

xfbs commented Apr 15, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants