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

String literal #2206

Open
ElectricCoffee opened this Issue Nov 4, 2017 · 8 comments

Comments

Projects
None yet
8 participants
@ElectricCoffee
Copy link

ElectricCoffee commented Nov 4, 2017

Constantly writing "foo".to_string() and String::from("foo") gets tedious after a while.

So I'm proposing a String literal, it could be something as simple as s"foo", @"foo", or even s!"foo" similar to vec![1, 2, 3].

Pros: String gets less verbose, and it comes across just as clear.

Cons: String gets special treatment

@Ixrec

This comment has been minimized.

Copy link
Contributor

Ixrec commented Nov 4, 2017

For reference, I believe the most recent discussion on the string literals problem was: https://internals.rust-lang.org/t/pre-rfc-allowing-string-literals-to-be-either-static-str-or-string-similar-to-numeric-literals/5029 Sadly this problem didn't quite fit into this year's ergonomics initiative.

@burdges

This comment has been minimized.

Copy link

burdges commented Nov 4, 2017

It frequently improves performance slightly if the code for which you need the .to_string() could take a T: AsRef<str> instead of String, which notably includes T = Cow<'static,str>.

As an aside, I'm curious : Is .to_owned()/.to_string() immediately followed by reserve(..) optimized into a single allocation? If not, there is a case for a ToString::to_string_with_reserve method, but really one should focus on that optimization there so that to_owned() and hence Cow<'a,[T]> benefit.

@scottmcm

This comment has been minimized.

Copy link
Member

scottmcm commented Nov 7, 2017

New string prefixes are a breaking change in the presence of macro_rules, IIRC. Is that the kind of thing that could be changed in an epoch? s"Hello" seems perfectly reasonable...

@ElectricCoffee

This comment has been minimized.

Copy link
Author

ElectricCoffee commented Nov 7, 2017

It also fits the style of raw strings, being r"Hello", so it's not exactly foreign syntax.

The logic behind s!"Hello" would basically be to allow " to be a legal delimiter in macros, thus allowing users to write their own string macros. Much like how Scala has custom string interpolation.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Nov 7, 2017

Long ago, rust had ~"" for what we'd now call a String literal. It caused a lot of people to use String where they didn't need to. Many people are opposed to a single-character String literal for this reason.

@warlord500

This comment has been minimized.

Copy link

warlord500 commented Mar 29, 2018

as much as this might be nice for new comers, i really don't think this would be a good idea!
all this would do is delay the inevitable. their eventually going to have to understand the difference between
str and String. dealing with that might actually make things now much harder!

@deifactor

This comment has been minimized.

Copy link

deifactor commented Dec 29, 2018

FWIW, one place where I tend to want this is when I've got something like:

map.get(key).cloned().unwrap_or("some default".to_owned())

and other similar cases where I want to use a string literal as a default value.

@ElectricCoffee

This comment has been minimized.

Copy link
Author

ElectricCoffee commented Dec 29, 2018

FWIW, one place where I tend to want this is when I've got something like:

map.get(key).cloned().unwrap_or("some default".to_owned())

and other similar cases where I want to use a string literal as a default value.

Personally, I prefer just using .into() wherever possible.
Granted, it doesn't convey intent as easily, as .into() could mean anything, but it's shorter than .to_owned() and .to_string().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.