-2

I write codes for add item in storage but when I run this command cargo build --release it show me this error :

error[E0308]: `match` arms have incompatible types
     --> /home/kianoush/projects/dortaj_pay/pallets/bussines/src/lib.rs:248:6
      |
  230 | /             match _itemInfo{
  231 | |                 _ => Err(<Error<T>>::StoreItemNotFound),
      | |                      ---------------------------------- this is found to be of type `Result<_, pallet::Error<T>>`
  232 | |                 Some(item) => {
  233 | |                     let user_basket_item = UserBasketItemItem {
  ...   |
  248 | |                     <Baskets<T>>::insert(storeId, store_basket);
      | |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Result`, found `()`
  249 | |                 }
  250 | |             }
      | |_____________- `match` arms have incompatible types
      |
      = note:   expected enum `Result<_, pallet::Error<T>>`
              found unit type `()`
  note: return type inferred to be `Result<_, pallet::Error<T>>` here

this is my code :

match _itemInfo{
    None => Err(<Error<T>>::StoreItemNotFound),
    Some(item) => {
        let user_basket_item = UserBasketItemItem {
            itemId: item_id,
            price: item.price,
            count: count_item,
        };

        let mut add_item: Vec<UserBasketItemItem> = Vec::new();
        add_item.push(user_basket_item);

        let store_basket = Basket {
            itemId: Self::gen_dna(),
            confirmPay: false,
            customerPay: false,
            items: add_item,
        };
        <Baskets<T>>::insert(storeId, store_basket);
    }
}

what is the problem ? how can i solve that ?

5
  • First of all that is a normal rust error, the match arms must return same types, first resolve that and convert the error type to Pallet::Error type None => Err(<Error<T>>::StoreItemNotFound.into()), And make sure the there is enum variant StorageItemNotFound in your error enum. Oct 22, 2022 at 11:26
  • @MrishoLukamba still show me error Oct 22, 2022 at 11:31
  • Please read the error message. Its very clear what you have to do. Your match arms has different return types Oct 22, 2022 at 11:36
  • @MrishoLukamba I Read that but i dont know how can i solve that Oct 22, 2022 at 11:38
  • Code related questions can only be properly answered when the full code is present. Please add a link to GitHub or some other public code hoster with the complete example, not just a snippet, thanks. Oct 22, 2022 at 17:14

2 Answers 2

2
`match` arms have incompatible types

That's the reason. This is a pure Rust problem. So, I edited the tags.

let foo = match bar {
  "baz" => Err(()),
  // The previous return type is a `Result`, so you also need to return a `Result` here
  "qux" => Ok(()),
}
let foo = match bar {
  // If you want to ignore the `Result`, make it returning empty here
  "baz" => { Err(()); }
  "qux" => (),
}
4
  • thanks for answer but my problem for this error : ` ^^^^^^ expected (), found enum Result ` Oct 22, 2022 at 11:55
  • still show me this error Oct 22, 2022 at 11:55
  • That is the error. And the reason is what I said. Did you try to update the code as I wrote?
    – AurevoirXavier
    Oct 22, 2022 at 15:28
  • 1
    Yes I did , but i did Oct 22, 2022 at 17:58
1

This is a pure rust problem both arms of the match must return the same type. I assume you intend to return the result of this match

but since

        <Baskets<T>>::insert(storeId, store_basket);

ends with a ; this arm will return () instead of a Result< ... > remove this and the return type will be that from the last statement in the block, hopefully the type you are expecting.

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