Skip to content

Literals don't live long enough #18852

@mahkoh

Description

@mahkoh
fn main() {
    let x = "".as_slice();
}
test5.rs:2:13: 2:15 error: borrowed value does not live long enough
test5.rs:2     let x = "".as_slice();
                       ^~
test5.rs:1:11: 3:2 note: reference must be valid for the block at 1:10...
test5.rs:1 fn main() {
test5.rs:2     let x = "".as_slice();
test5.rs:3 }
test5.rs:2:5: 2:26 note: ...but borrowed value is only valid for the statement at 2:4
test5.rs:2     let x = "".as_slice();
               ^~~~~~~~~~~~~~~~~~~~~
test5.rs:2:5: 2:26 help: consider using a `let` binding to increase its lifetime
test5.rs:2     let x = "".as_slice();
               ^~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error

Similarly you can't pass an array literal to BufReader.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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