0

So I have this code here in my vesting pallet:

        #[pallet::weight(100)]
        pub fn modify_vesting_scheudule_block_for_all (
            origin: OriginFor<T>,
            block_amount: <T as frame_system::Config>::BlockNumber,
        ) -> DispatchResultWithPostInfo {
            ensure_root(origin)?;
            let res = with_transaction_result (|| {
                let mut vested_ids = Vec::new();
                for (id,_schedules) in VestingSchedules::<T>::iter() {
                    vested_ids.push(id);
                }
                for id in vested_ids.iter() {
                    let schedules = VestingSchedules::<T>::take(id);
                    let mut tmp: BoundedVec<VestingScheduleOf<T>, T::MaxVestingSchedules>;
                    for schedule in &schedules {
                        if schedule.start < block_amount {
                            // need to put it back and revert the take if there is an error 
                            // VestingSchedules::<T>::insert(id, schedules.clone());
                            return Err(Error::<T>::InvalidClaimBlock.into());
                        };
                        // ensure!(schedule.start > block_amount, Error::<T>::InvalidClaimBlock);
                        tmp.push(
                            VestingSchedule {
                                start: schedule.start - block_amount,
                                period: schedule.period,
                                period_count: schedule.period_count,
                                vested_amount: schedule.vested_amount,  
                            }
                        );
                    }
                    VestingSchedules::<T>::insert(id, tmp);
                }   
                Ok(())
            });
            
            match res {
                Ok(_) => {
                    Self::deposit_event(Event::VestingScheduleModified(block_amount));
                    Ok(().into())
                },
                Err(_e) => {
                    Err(Error::<T>::InvalidClaimBlock.into())
                }
            }
            
        }

However I am getting this error:

error[E0381]: borrow of possibly-uninitialized variable: `tmp`
   --> pallets/vesting/src/lib.rs:269:7
    |
269 | /                         tmp.push(
270 | |                             VestingSchedule {
271 | |                                 start: schedule.start - block_amount,
272 | |                                 period: schedule.period,
...   |
275 | |                             }
276 | |                         );
    | |_________________________^ use of possibly-uninitialized `tmp`

This makes sense to me but I have been searching around and I can not seem to find any documentation on how to actually intialize an empty bounded Vec. I have been using this as reference: https://docs.rs/bounded-vec/latest/bounded_vec/struct.BoundedVec.html. How do I initialize a bounded Vec?

1 Answer 1

2

As with all types in Rust that can be default initialized you can use the Default trait:

let mut my_vec: BoundedVec<u8, ConstU32<10>> = Default::default();

Or equivalently

let mut my_vec = BoundedVec::<u8, ConstU32<10>>::default();

You can also use the macro

let mut my_vec: BoundedVec<u8, ConstU32<10>> = bounded_vec![];

Or omit the type and hope that the compiler figures it out:

let mut my_vec = bounded_vec![];
1
  • Thank you! Default::default(); worked like a charm Aug 17, 2022 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.