# \[pre-RFC\] Allowing string literals to be either &'static str or String, similar to numeric literals

**URL:** <https://internals.rust-lang.org/t/pre-rfc-allowing-string-literals-to-be-either-static-str-or-string-similar-to-numeric-literals/5029>\
**Category:** Uncategorized\
**Created:** [April 1, 2017, 12:16pm UTC](https://internals.rust-lang.org/t/pre-rfc-allowing-string-literals-to-be-either-static-str-or-string-similar-to-numeric-literals/5029 "2017-04-01T12:16:21Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [April 1, 2017, 12:16pm UTC](https://internals.rust-lang.org/t/pre-rfc-allowing-string-literals-to-be-either-static-str-or-string-similar-to-numeric-literals/5029/1 "2017-04-01T12:16:21Z")

</div>

[This thread](https://internals.rust-lang.org/t/pre-rfc-improving-the-ergonomics-of-creating-owned-string-objects/5028) reminded me that we haven’t had a serious discussion of the string literal to String problem in a while, and I haven’t seen anyone propose this particular solution yet.

## Summary

String literals can be either &'static str or String. Type deduction attempts to pin down which type each literal should be, and if it can’t then it falls back on assuming &'static str.

This is similar to how numeric literals can be any numeric type, but fall back to i32.

## Motivation

Basically, having to write these .to\_string()s is really annoying:

```
fn foo(v: &Vec<String>) {
    println!("foo: {:?}", v);
}

fn main() {
    let v = vec!["x".to_string(), "y".to_string(), "z".to_string()];
    foo(&v);
}

```

They probably also confuse beginners, and force beginners to confront the nuances of the various string types earlier than perhaps they should. And they’re an annoying distraction to everyone who already knows the difference between &'static str and String.

One obvious solution is introducing implicit conversions from &'static str to String, but that would be very undesirable for a language like Rust that cares about performance as well as ergonomics.

## Detailed Design

The same as numeric literals, except we have only two types that we want string literals to be: `&'static str` and `String`.

That means the following code should just work:

```
fn foo(v: &Vec<String>) {
    println!("foo: {:?}", v);
}
fn bar(v: &Vec<&'static str>) {
    println!("bar: {:?}", v);
}

fn main() {
    let v1 = vec!["x", "y", "z"]; // v1 is a Vec<String>
    foo(&v1);
    let v2 = vec!["x", "y", "z"]; // v2 is a Vec<&'static str>
    bar(&v2);
}

```

When there is no explicit type available, we fall back to &'static str, so code like this which works today will continue to work:

```
fn foo<T>(v: &Vec<T>) where T: std::fmt::Debug {
    println!("foo: {:?}", v);
}

fn main() {
    let v = vec!["x", "y", "z"]; // v continues to be a Vec<&'static str>
    foo(&v);
}

```

This proposal emphatically does not include any auto-ref, auto-deref or implicit conversions. The only impact is that some string literals would be treated as “String literals” instead of “&'static str” literals, and only in cases where the code wouldn’t compile otherwise.

## How We Teach This

We have to teach Rust users about &str, &'static str and String at some point no matter what. The biggest impact of this change on teaching is that we don’t have to immediately delve into that subject or unsatisfyingly handwave it away the first time we show a program with a string literal in it.

## Drawbacks

The type of a string literal would become context-dependent, which could be considered confusing. Whether that’s more confusing than the status quo is debatable.

Today, no type deduction is required on string literals. Introducing type deduction here may create cases of “inference breakage”.

## Alternatives

Explicit [prefixes](https://internals.rust-lang.org/t/pre-rfc-string-literals-through-prefixes/2928), suffixes, operators, methods with shorter names than to\_string(), or [alternate quotation marks](https://internals.rust-lang.org/t/pre-rfc-improving-the-ergonomics-of-creating-owned-string-objects/5028). I’m grouping all of these together because they’re all various kinds of _explicit annotation_. Because I believe this frustration is unique to string literals, it’s usually very easy to infer when those literals have to be Strings, and &'static str is an obvious choice for the default string literal type (more obvious than i32 was for numeric literals imo), I see no reason to require an annotation at all.

As previously mentioned, implicit conversions are another option.

Doing nothing is always an option.

## Unresolved Questions

How much, if any, code would experience “inference breakage” under this change? (I couldn’t think of anything but I’m sure one of you can)

Are there any other “string types” we might want string literals to be deduced to?

---

_[View the full topic](https://internals.rust-lang.org/t/pre-rfc-allowing-string-literals-to-be-either-static-str-or-string-similar-to-numeric-literals/5029)._
