Skip to content

Deref coercion from String to &str doesn't seem to always work #22649

@Valloric

Description

@Valloric

This code compiles fine:

fn main() {
  let x = "a".to_string();
  let y: String = ["b", &x[..], "c"].concat();
  println!("{}", y);
}

but this code doesn't

fn main() {
  let x = "a".to_string();
  // Doesn't work
  let y: String = ["b", &x, "c"].concat();
  println!("{}", y);
}

error output:

<anon>:4:25: 4:27 error: mismatched types:
 expected `&str`,
    found `&collections::string::String`
(expected str,
    found struct `collections::string::String`) [E0308]
<anon>:4   let y: String = ["b", &x, "c"].concat();
                                 ^~

I'd expect to not have to sometimes write &foo[..] and sometimes &foo to get a &str out a String. The longer form is too verbose, and the inconsistency of having to sometimes use one over the other seems like a needless user mental model cost, especially because it's not obvious to me at all when I should use one form over the other.

rustc version: rustc 1.0.0-nightly (522d09dfe 2015-02-19) (built 2015-02-21)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions