Iterate through an array and check if text exists in sub object in rust

I have an object

    cart: {
        delivery_groups: [{
            selected_delivery_option: {
                title: "Test"
    payment_methods: [{
        id: "1",
        name: "Test"
    payment_customization: {
        metafield: {
            value: "{\"paymentMethod\": \"Test\", \"shippingMethod\": \"Test\"}"

I want to check

cart.delivery_groups.iter().selected_delivery_option.title == 'Test'

is true or false.

How to do that in rust.

You can use Iterator::any.

    .any(|x| x.selected_delivery_option.title == "Test")
1 Like

it shows an error

error[E0609]: no field `title` on type `std::option::Option<InputCartDeliveryGroupsSelectedDeliveryOption>`

when i did

println!("selected delivery option {:?}", x.selected_delivery_option);

got the below output

selected delivery option Some(InputCartDeliveryGroupsSelectedDeliveryOption { title: Some("Another delivery method") })

In this case, you can use Option::and_then to get the value of the title field.

x.selected_delivery_option.and_then(|x| x.title) == Some("Test")
^^^^^^^ move occurs because `x.title` has type `std::option::Option<std::string::String>`, which does not implement the `Copy` trait
method.selected_delivery_option.and_then(|x| x.title) == Some("Test".to_string())
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --------------------- `method.selected_delivery_option` moved due to this method call
     |             |
     |             help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
     |             move occurs because `method.selected_delivery_option` has type `std::option::Option<InputCartDeliveryGroupsSelectedDeliveryOption>`, which does not implement the `Copy` trait
  --> src/
44 |             method.selected_delivery_option.as_ref().and_then(|x| x.title) == Some("Test".to_string())
   |                                                                   ^^^^^^^ move occurs because `x.title` has type `std::option::Option<std::string::String>`, which does not implement the `Copy` trait

Generally the compiler give you the solution.
Here you can take the field by ref.

Are you new to rust ? Those issues are pretty trivial to solve. I can only suggest you to read the book. It will help you to understand the issues and how to solve them.

1 Like

yeah.. i am new to rust and implementing shopify extension using rust Add configuration to your payment customization

let selected = method.selected_delivery_option.as_ref().unwrap();
            selected.title ==  Some(config.shipping_method.to_string())

Is this correct way of coding ?

this is working. need to add test cases....

Using unwrap is a bad practice because it will make your code panic if the value is None.
Use and_then instead. Or alternatively you may use if-let but the code will be more verbose.

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.