Trying to understand Rc / Chaining

โš“ Rust    ๐Ÿ“… 2025-06-27    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 8      

surdeus

Warning

This post was published 43 days ago. The information described in this article may have changed.

Hi.
Iโ€™m trying to understand Rc but am having trouble finding information of wether what I am trying is even possible. Chaining functions is a common Pattern in Rust, however I am unsure of how it interacts with Rc. Specifically, I am currently working on a problem where I have two Structs, and one is an extension of the other with only one information added. Instances of TypeA will not change during runtime once they were created and their number is limited. TypeB is derived from an instance of TypeA, and there will be many instances of TypeB created and put in a further datastructure, so I understand using an Rc is desirable to avoid .clone(), for both runtime and memory reasons.

I was trying to implement this using the builder pattern, but the naive operation obviously doesnโ€™t work, because self will be moved:

#[derive(Default)]
struct TypeA {
   very_important_data: Vec<u32>,
   some_other_data: u32,
}

struct TypeB {
   data: Rc<TypeA>,
   position: u64,
}
impl TypeA {
   fn into_type_b(self, pos: u64) -> TypeB {
      TypeB {
         position: pos,
         data: Rc::from(self),
      }
   }
}
_____
let a = TypeA::default();
let b = a.into_type_b(0);
//This next line cannot compile because a was moved above
let b2 = a.into_type_b(1);
_______
///This compiles, but doesnโ€™t follow the pattern I was hoping was possible
fn into_type_b(a: Rc<TypeA>, pos: u64) {
   TypeB {
      position: pos,
      data: a,
   }
}

I tried a few other things such as using &self or trying to implement for impl Rc<TypeA> but neither of that seemed to work either.

Is it possible to follow that pattern with Rc at all?

Of course one might ask the question of why not use a simple reference: data: &'a TypeA, but that quickly escalates with an exponentionally growing number of lifetime annotations throughout the code and that is frankly beyond my Rust level of competence for now.

Thanks for reading and any help

3 posts - 3 participants

Read full topic

๐Ÿท๏ธ rust_feed