将对一个结构的一部分的引用返回为另一结构的字段 [英] Return references to parts of one struct as fields of another struct

查看:40
本文介绍了将对一个结构的一部分的引用返回为另一结构的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,我想将一个结构移到另一个结构中,并在不克隆或复制的情况下将第一个结构的一部分作为其他结构的一部分获得引用.如何以正确的方式做到这一点?

I want move one struct into another and get references on parts of first struct as parts of other without cloning or copying, if it is possible. How to do it in right way?

fn main() {
    let foo = Foo::new();
    let bar = Bar::new(foo);
    println!("{:?}", bar);
}

#[derive(Debug)]
struct Foo {
    v: String,
}

impl Foo {
    pub fn new() -> Self {
        Foo {
            v: String::from("a|b"),
        }
    }

    pub fn get_a(&self) -> &str {
        &self.v[0..1]
    }

    pub fn get_b(&self) -> &str {
        &self.v[2..3]
    }
}

#[derive(Debug)]
struct Bar<'a> {
    foo: Foo,
    a: &'a str,
    b: &'a str,
}

impl<'a> Bar<'a> {
    pub fn new(f: Foo) -> Self {
        Bar::parse(f)
    }

    fn parse(f: Foo) -> Self {
        let a = f.get_a();
        let b = f.get_b();

        Bar { foo: f, a, b }
    }
}

铁锈操场

我遇到一个错误:

error[E0515]: cannot return value referencing function parameter `f`
  --> src/main.rs:44:9
   |
41 |         let a = f.get_a();
   |                 - `f` is borrowed here
...
44 |         Bar { foo: f, a, b }
   |         ^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function

error[E0515]: cannot return value referencing function parameter `f`
  --> src/main.rs:44:9
   |
42 |         let b = f.get_b();
   |                 - `f` is borrowed here
43 | 
44 |         Bar { foo: f, a, b }
   |         ^^^^^^^^^^^^^^^^^^^^ returns a value referencing data owned by the current function

error[E0505]: cannot move out of `f` because it is borrowed
  --> src/main.rs:44:20
   |
35 | impl<'a> Bar<'a> {
   |      -- lifetime `'a` defined here
...
41 |         let a = f.get_a();
   |                 - borrow of `f` occurs here
...
44 |         Bar { foo: f, a, b }
   |         -----------^--------
   |         |          |
   |         |          move out of `f` occurs here
   |         returning this value requires that `f` is borrowed for `'a`

推荐答案

返回 parse 的参数 f parse 的生​​存期结束.较旧的Rust编译器版本返回了一条错误消息,该消息可能更有用:

The lifetime of the argument f to parse ends when parse returns. Older Rust compiler versions returned an error message which might have been more useful:

error[E0597]: `f` does not live long enough
  --> t.rs:41:17
   |
41 |         let a = f.get_a();
   |                 ^ borrowed value does not live long enough
...
45 |     }
   |     - borrowed value only lives until here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the impl at 35:1...
  --> t.rs:35:1
   |
35 | / impl<'a> Bar<'a> {
36 | |     pub fn new(f: Foo) -> Self {
37 | |         Bar::parse(f)
38 | |     }
...  |
45 | |     }
46 | | }
   | |_^

我可以通过将 Bar 的定义更改为以下内容来编译您的示例:

I can get your example to compile by changing the definition of Bar to:

#[derive(Debug)]
struct Bar<'a> {
    foo: &'a Foo,
    a: &'a str,
    b: &'a str,
}

,并将类型为&'a Foo 的引用传递给 Bar :: new Bar :: parse .但是,尚不清楚此解决方案是否可以解决您的原始问题.如果所有权结构太复杂,也许您需要使用 Rc .

and passing references of type &'a Foo to Bar::new and Bar::parse. However, it is unclear if this solution will work for your original problem. Maybe you need to use Rc if the ownership structure is too complicated.

这篇关于将对一个结构的一部分的引用返回为另一结构的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆