Getting value from field

This may be an easy one, but i am new to Rust, so some help would be nice.

How do i get the value in the "name" field in the struture below?

First Blob name: Blob(Blob { name: "-ST-20230607-0926.tar", snap ......

https://docs.rs/azure_storage_blobs/0.20.0/azure_storage_blobs/container/operations/list_blobs/enum.BlobItem.html

Below is the code i have used to get out the entire Blob struture

let mut stream = blob_list.into_stream();
    while let Some(value) = stream.next().await {
        let mut body = value?.blobs;
        match body.items.get(0) {
            Some(first_blob) => println!("First Blob name: {:?}", first_blob),
            None => println!("No blobs found."),
        }
    }

I hope this is enough to go on

Not familiar with the azure_storage_blobs crate, but guessing from the docs, maybe this could work?

        match body.items.get(0) {
            Some(first_blob) => match first_blob {
                BlobItem::Blob(blob) => println!("First Blob name: {:?}", blob.name),
                BlobItem::BlobPrefix(blob_prefix) => println!("First Blob name: {:?}", blob_prefix.name),
            }
            None => println!("No blobs found."),
        }

Thanks, that works.

So the program, has to know how to Blob i struture by using the BlobItem enum? is that correct understood?

I can't give you any insights into the design decisions and the resulting structure of types of the crate you are using as I didn't develop the crate. All I did was look at the type signatures, saw that Blobs contains a field items that holds Vec<BlobItem>. Then I checked what BlobItem looks like and it is indeed an enum with two variants, one holding onto a Blob, the other only to a BlobPrefix (no idea what that is in azure storage terms). But both have a name field, so we can use that to print the name. What the meaning behind those types are, I can only guess.

1 Like

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.