Skip to content

slice of static lifetime elements, contains() requires operand to have static lifetime too #18214

Closed
@mikedilger

Description

@mikedilger

I'm not sure if this is a bug, or if it is already known, or if I'm just too dense to grok this stuff, but it seems to me that the lifetime of my needn't be any longer than the contains() call itself. Yet we have the lifetime error.

fn words() -> Vec<&'static str>
{
    vec!["these","are","my","words"]
}

fn main() {
    let my = "my".into_string();
    if words().contains(&my.as_slice()) {
        println!("Its in there!");
    }
    else {
        println!("Word not found");
    }
}
demo.rs:9:26: 9:28 error: `my` does not live long enough
demo.rs:9     if words().contains(&my.as_slice()) {
                                   ^~
note: reference must be valid for the static lifetime...
demo.rs:7:11: 15:2 note: ...but borrowed value is only valid for the block at 7:10
demo.rs:7 fn main() {
demo.rs:8     let my = "my".into_string();
demo.rs:9     if words().contains(&my.as_slice()) {
demo.rs:10         println!("Its in there!");
demo.rs:11     }
demo.rs:12     else {
           ...
error: aborting due to previous error

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

    Issue actions