Skip to content

Compilation error when trying to drop with cyclic lifetimes #50

@uselessgoddess

Description

@uselessgoddess
//use beef::lean::Cow; -- compile error
use std::borrow::Cow;

struct Data<'a> {
    data: String,
    follower: Option<Cow<'a, str>>,
}

fn main() {
    let mut data = Data {
        data: "hello".into(),
        follower: None,
    };
    data.follower = Cow::from(&data.data).into();
}

In this code, follower has lifetime 'data and data has lifetime 'data
If i use beef::Cow – compiler tells me:

error[E0597]: `data.data` does not live long enough
  --> src\main.rs:14:31
   |
14 |     data.follower = Cow::from(data.data.as_str()).into();
   |                               ^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
15 | }
   | -
   | |
   | `data.data` dropped here while still borrowed
   | borrow might be used here, when `data` is dropped and runs the destructor for type `Data<'_>`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions