无法使用内部引用移动结构

时间:2019-08-02 03:12:14

标签: rust borrow-checker

我正在制作一些示例Rust代码,以显示Rust中的结构如何具有引用其他字段的字段,只要我不移动该结构,它就可以正常工作。

invoiceIndex

由于如果我用$group本身替换对time <- c("2015","2016","2017","2015","2016","2017") kpi <- c(4,5,3,3,4,5) store <- c("A","B","C","A","B","C") data <- data.frame(store,kpi,time) 的调用,它可以按预期工作,所以我认为移动该结构不会有任何不同。但是,存在编译错误:

#[derive(Debug)]
struct Bar {
    x: i64,
}

#[derive(Debug)]
struct Foo<'a> {
    bar: Bar,
    bar_ref: Option<&'a Bar>,
}

fn print_foo(foo: Foo) {
    println!("foo is {:?}", foo);
}


fn main() {
    let mut foo = Foo {bar: Bar{x: 3}, bar_ref: None};
    foo.bar_ref = Some(&foo.bar);
    print_foo(foo);
}

我使用的是以下版本:

print_foo

0 个答案:

没有答案
相关问题