Move occurs when try to push Box<Node> in Vec

struct Node {
    	degree: usize,

impl Default for Node{
	fn default() -> Node{
		let vec:Vec<Box<Node>> = Vec::new();

   impl Node{
     	pub fn connect_with(&mut self,node:Node){

	let heap_node = Box::new(node);
pub fn increment_degree(&mut self){;

pub fn decrement_degree(&mut self){

am getting error "move occurs because self.connection has type std::option::Option<std::vec::Vec<std::boxed::Box<eulerian::Node>>>, which does not implement the Copy trait
| help: consider borrowing the Option's content: self.connection.as_ref()"



But when I try to use self.connection.as_ref().unwrap().push(heap_node);
I get follwing error

cannot borrow data in a & reference as mutable

The mutable equivalent of Option::as_ref is Option::as_mut.

thank you

1 Like

Just a sidenote: you don't need a Box when you put the values in a Vec (if that's just because of the small example ignore this :slight_smile: )


@raidwas you are talking about this.. can you please explain little more about it

Practically you don't need type signature like Vec<Box<T>>. Actually, in Rust the Box<T> is not needed usually. The only practical case for many people that the Box<T> actually solve the problem is when they want to define recursive types like struct Node<T>(T, Option<Box<Node<T>>>).

1 Like

Can you give me some online example so that I can get to know what recursive types you mean here.

So this example using Box I can say this right way.

Linked lists and trees are the most common examples of recursive type. For the linked list part, there's an awesome article called Learn Rust With Entirely Too Many Linked Lists.

1 Like

@Hyeonu thank you

@Hyeonu I read this reddit post
I want to achieve same thing ..have vector of references ..I just want to ask ...cannot it be done using Box instead of using lifetime to node?

@ninja0x0 you may read more about smart pointers.

According to the Rust Book, Vec and Box are both smart pointers (

They implement special behaviour to referencing, as memory management is done by ownership. More examples of smart pointers you might be interested are Rc (to provide multiple references to the same object) and Refcell (to provide inner mutability). Composition of smart pointers is possible and eventually necessary, but not all of them are.

Collections ( like Vec which you might be interested in and are useful are HashSet, HashMap, LinkedList.

1 Like

I read about smart pointers.I will look into collections implementations.But can you tell me what is proper implementation of my small snippet code which I posted above.

@ninja0x0 Well, I'm in no position greater than anybody here to say what is proper or not. Much probably, someone can answer you with more property if I miss something. :sweat_smile:

What I understand from the snippet you've provided is that you want a Node to reference as many nodes the user needs. And it is implicit that a Node may be referenced according to the user's will as well.

The std::rc::Rc is required, which will allow us to have any node to be pointed by as many nodes as we want.

Also, for a Node to point to more than one node, we need some Collection. To avoid unnecessary complications, we may use the most common and standard Vec.

use std::rc::Rc;

struct Node {
    degree: usize,
    connection: Vec<Rc<Node>>,
    value: Option<i32>,

impl Default for Node {
    fn default() -> Node {
        Node {
            degree: 0,
            connection: Vec::new(),
            value: None,

impl Node {
    pub fn connect_with(&mut self, node: &Rc<Node>) {
    pub fn increment_degree(&mut self) { += 1;

    pub fn decrement_degree(&mut self) {
        if > 0 {
   -= 1;

fn main() {
    // Creating Nodes
    let root = Rc::new(Node::default());
    let n1 = Rc::new(Node::default());
    let n2 = Rc::new(Node::default());
    let n3 = Rc::new(Node::default());

    // Making connectiion between nodes
1 Like

The RefCell is not necessary to implement interior mutability, in this case, since Vec already provides it.

Someone may confirm it. I believe I've read it somewhere else.

1 Like

Thank you so much for your solution.I appreciate your time @nlsn

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.