Skip to content

String guide: make distinction between str and &str #21035

Closed
@mdinger

Description

@mdinger
Contributor

The string guide distinguishes between String and &str but makes no mention of a third type people will come across: str. &str is defined as the type which you refer to? Perhaps define str as the type and mention that in most cases it must be borrowed...

This honestly threw me the first time I saw it. I thought, "what is str...there's only &str and String ...".

Problematic case:

fn main() {
    let str_slice = "str slice";
    let mut string = String::new();

    // Doesn't work because `str_slice[0..3]` needs a `&`
    string = string + str_slice[0..3] + "ing";
    println!("{:?}", string); // Would print `string` without the error.
}

Error:

... error ... expected str, found &-ptr
<anon>:5     string = string + str_slice[0..3] + "ing";
                               ^~~~~~~~~~~~~~~

Activity

Manishearth

Manishearth commented on Jan 12, 2015

@Manishearth
Member

Note: str is an unsized type, and &str is simply a pointer to the unsized type (like &Trait or &[]). But I agree that the docs could be clearer.

jakerr

jakerr commented on Jan 29, 2015

@jakerr
Contributor

The error messages can be quite unfriendly for new users.

I recently saw someone attempt:

struct S {
    s: str
}

fn main() {
    let hi = "Hi";
    let s = S { s: *hi };
    println!("Hello errors!");
}

The error:

<anon>:7:13: 7:25 error: the trait `core::marker::Sized` is not implemented for the type `str` [E0277]
<anon>:7     let s = S { s: *hi };
                     ^~~~~~~~~~~~
<anon>:7:13: 7:25 note: `str` does not have a constant size known at compile-time
<anon>:7     let s = S { s: *hi };
                     ^~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101

Doesn't mean much to a new user.

Note: str is an unsized type, and &str is simply a pointer to the unsized type (like &Trait or &[]).

What does that mean to users? Is there a valid reason to ever use str in code?

The std::str doc says

While represented by the name str, the name str is not actually a valid type in Rust.

So it's not a valid type? But the error message you gave me said "for the type str" ... If it's not valid why doesn't the compiler say so in the error message above, instead of complaining that it's unsized?

steveklabnik

steveklabnik commented on Feb 2, 2015

@steveklabnik
Member

Today, this is the string chapter of the book.

added a commit that references this issue on Mar 23, 2015
1be8fcb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@jakerr@kmcallister@Manishearth@mdinger

        Issue actions

          String guide: make distinction between `str` and `&str` · Issue #21035 · rust-lang/rust