Skip to content

String Slices / Literals should not be mutable  #22733

Description

@ltfschoen

I am learning Rust by applying code from the Rust Docs (Nightly) Book.
In Chapter 2.11 Strings they explain that the &str Type of (aka String Slices / String Literals) have a fixed size and can NOT be mutated, whereas the String Type (aka In-Memory Strings) can be mutated.

Why then does Rust and Cargo allow me to declare a mutable variable with let mut named stringLiteral of &str Type here without giving a note, error, or warning in the console that this is not allowable?

Furthermore, why does Rust and Cargo allow me to build $ cargo build --verbose and run the app $ ./target/hello_world, which allows me to overwrite the initial value stored in stringLiteral here, but without giving a note, error, or warning in the console that this is not allowable. I have demonstrate that it has in fact been overwritten by printing its new value to the console [here][https://github.com/ltfschoen/RustTest/blob/master/src/main.rs#L139],

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    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