-
Couldn't load subscription status.
- Fork 13.9k
Description
UPDATE: This needs a test! Mentoring instructions can be found here.
While working on some code using std::borrow::Cow I encountered some strange behavior. Here is a simplified version of the code I was using:
use std::borrow::Cow;
#[derive(Clone, Debug)]
struct S<'a> {
name: Cow<'a, str>
}
#[derive(Clone, Debug)]
struct T<'a> {
s: Cow<'a, [S<'a>]>
}
fn main() {
let s1 = [S { name: Cow::Borrowed("Test1") }, S { name: Cow::Borrowed("Test2") }];
let b1 = T { s: Cow::Borrowed(&s1) };
let s2 = [S { name: Cow::Borrowed("Test3") }, S { name: Cow::Borrowed("Test4") }];
let b2 = T { s: Cow::Borrowed(&s2) };
let mut v = Vec::new();
v.push(b1);
v.push(b2);
println!("{:?}", v);
}
This fails with
error[E0597]:
s2does not live long enough
Removing b1,s1 or b2,s2 from the code fixes the error. However, since the very same pattern isused for both variable pairs it seems to be inconsistent behavior of the borrow checker.
Meta
Versions checked:
rustc 1.18.0 (03fc9d6 2017-06-06)
binary: rustc
commit-hash: 03fc9d6
commit-date: 2017-06-06
host: x86_64-pc-windows-gnu
release: 1.18.0
LLVM version: 3.9
rustc 1.19.0-beta.2 (a175ee5 2017-06-15)
rustc 1.20.0-nightly (734c836 2017-07-03)