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 guide: make distinction between str and &str #21035

Closed
mdinger opened this issue Jan 12, 2015 · 3 comments
Closed

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

mdinger opened this issue Jan 12, 2015 · 3 comments

Comments

@mdinger
Copy link
Contributor

mdinger commented Jan 12, 2015

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";
                               ^~~~~~~~~~~~~~~
@Manishearth
Copy link
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
Copy link
Contributor

jakerr commented Jan 29, 2015

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
Copy link
Member

Today, this is the string chapter of the book.

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

5 participants