# Blockchain Without Copy,Clone Block-Refcell

**URL:** <https://users.rust-lang.org/t/blockchain-without-copy-clone-block-refcell/77448>\
**Category:** help\
**Created:** [June 25, 2022, 1:23pm UTC](https://users.rust-lang.org/t/blockchain-without-copy-clone-block-refcell/77448 "2022-06-25T13:23:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![armanriazi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/armanriazi/32/27129_2.png) [@armanriazi](https://users.rust-lang.org/u/armanriazi)\
**Post date:** [June 25, 2022, 1:23pm UTC](https://users.rust-lang.org/t/blockchain-without-copy-clone-block-refcell/77448/1 "2022-06-25T13:23:04Z")

</div>

I have got a mystery that I decide to reuse produced lasthash for the next block without any Copy or Clone because of high safe code(because I think if we used Copy and Clone afterward the Blockchain will be Hk by DoubleXpends or OverXpend). I used Rc\<RefCell\> but I have got this error:  
(followed lines:  
refered1\_base\_maked\_block.prev\_block\_hash,  
refered1\_base\_maked\_block.option\_transactions,)  
I can preceded lines solve by adding new Rc\<RefCell for two types but I do not want do it.  
I'm stuck in a loop repeating this puzzle for all non-primitives types!  
What is solution instead of using Clone?

```rust
cannot move out of dereference of `RefMut<'_, library_blockchain::Block>`
move occurs because value has type `Vec<OptionTransaction>`, which does not implement the `Copy` trait

```

```rust
            //-------------Making Block
            let mut base_maked_block: Rc<RefCell<Block>> = Rc::new(RefCell::new(
                Block::new(i as u32, now(), last_hash.to_vec(), maked_transaction, difficulty)
            ));
            
            let mut refered1_base_maked_block=base_maked_block.borrow_mut();
            
            refered1_base_maked_block.mine();

            let refered1_block=Block::new(
                refered1_base_maked_block.index,
                refered1_base_maked_block.timestamp,                
                refered1_base_maked_block.prev_block_hash,                
                refered1_base_maked_block.option_transactions,
                refered1_base_maked_block.difficulty
            );
            let b=*refered1_base_maked_block;
            blockchain.update_with_block(b).expect("\n\nFailed to add genesis block");    
            
            let refered2_base_maked_block=base_maked_block.borrow();
            
            last_hash =refered2_base_maked_block.prev_block_hash.to_vec().into_boxed_slice();
            
            println!(" **last hash new:** \n{:?}\n",&last_hash);

```

Types:

```rust
pub struct Block {
    pub index: u32,
    pub timestamp: u128,
    pub hash: Hash,
    pub prev_block_hash: Hash,
    pub nonce: u64,
    pub option_transactions: Vec<OptionTransaction>,
    pub difficulty: u128, 
}

```

Repo[source code branch armanriazi](https://github.com/armanriazi/rust-scratch-blockchain)

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [June 26, 2022, 2:15pm UTC](https://users.rust-lang.org/t/blockchain-without-copy-clone-block-refcell/77448/3 "2022-06-26T14:15:13Z")

</div>

I feel like the question you have could be formulated more clearly, and with a more descriptive title; feel free to edit your post accordingly, or post additional reply with more information.

One first step of improvement would be to post a _complete error message_ (including things such as the information _where in the code_ the error occurred) as e.g. returned from running `cargo check` in the terminal.

---

<div class="post-metadata">

**Author:** ![Cerber-Ursi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cerber-ursi/32/8315_2.png) [@Cerber-Ursi](https://users.rust-lang.org/u/Cerber-Ursi)\
**Post date:** [June 27, 2022, 4:42am UTC](https://users.rust-lang.org/t/blockchain-without-copy-clone-block-refcell/77448/4 "2022-06-27T04:42:34Z")

</div>

How is `refered1_block` used after creation? Can it be the same block as the `refered1_base_maked_block` (i.e. the clone of the same `Rc`)? If it can't, then the cloning of the vector is semantically unavoidable.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 25, 2022, 4:42am UTC](https://users.rust-lang.org/t/blockchain-without-copy-clone-block-refcell/77448/5 "2022-09-25T04:42:55Z")

</div>

Closed on September 25, 2022, 4:42am UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
