Get one data member from a struct

I have a function that returns a struct to me like this:

MerkleTree { hasher: DefaultHasher { Sha256 }, nodes: [[232, 95, 54, 119, 251, 80, 102, 127, 47, 204, 60, 202, 7, 7, 249, 71, 205, 215, 149, 242, 95, 226, 20, 140, 247, 176, 61, 122, 64, 223, 9, 22], [71, 46, 102, 197, 27, 201, 138, 41, 206, 167, 129, 77, 69, 203, 139, 239, 183, 216, 109, 144, 27, 80, 103, 154, 95, 156, 191, 150, 51, 193, 96, 194], [71, 46, 102, 197, 27, 201, 138, 41, 206, 167, 129, 77, 69, 203, 139, 239, 183, 216, 109, 144, 27, 80, 103, 154, 95, 156, 191, 150, 51, 193, 96, 194]], count_internal_nodes: 1, count_leaves: 2 }

It has 4 data members inside the struct. What can I do if I want only one member, say nodes to be returned?

Here is the code that produces the above output:

main.rs:

use merkle_tree::MerkleTree;


fn main() {
    let block = "Merkle Tree";
    let merkle_tree: MerkleTree = MerkleTree::build(&[block, block]);
    println!("{:?}", merkle_tree);
}

Cargo.toml:

[dependencies]
merkle_tree = {git = "https://github.com/melekes/merkle-tree-rs"}

You cannot. The struct's members are not marked pub, so they cannot be accessed outside it's module.

Isn't there a way to manipulate the response somehow?

You could fork the library, make accessors functions or mark the fields pub (I don't reccommend the latter though) and then use them.
Given the library, you cannot do anything.

Note that Debug formatting ({:?}) is not meant to be relied on for anything whatsoever except debugging — its formatting and contents may change without notice. If you need predictable results, the type should offer another way to get them.

1 Like

Specifically, it's part of the contract of a crate that the implementor can change any detail that isn't pub.

Fortunately, there are a crapload of other options

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.