0
votes

Crate sha2 contains the type Sha256 which implements the trait Digest defined in the crate digest. This trait is also reexported from sha2.

I want to write a file which doesn't mention sha2, just digest:

merkle_tree.rs:

use digest::Digest;

#[derive(Default)]
pub struct MerkleTree<T: Digest> {
    digest: T,
}

impl<T: Digest+Default> MerkleTree<T> {
    pub fn new() -> MerkleTree<T> {
        MerkleTree{ ..Default::default() }
    }
}

main.rs:

extern crate digest;
extern crate sha2;

mod merkle_tree;

use sha2::{Digest, Sha256};
use merkle_tree::MerkleTree;

fn main() {
    let mut mt = MerkleTree::<Sha256>::new();
    println!("Hello, world!");
}

I have the following output:

error: no associated item named new found for type merkle_tree::MerkleTree<sha2::Sha256> in the current scope the trait digest::Digest is not implemented for sha2::Sha256

Cargo.toml:

[package]
name = "merkle_tree"
version = "0.1.0"
authors = ["Simon Prykhodko <[email protected]>"]

[dependencies]
digest = "0.4.0"
sha2 = "0.3.0"

What's wrong here?

2
@ababo Why are you using sha2 = "0.3.0"? The latest version is "0.4.2". - kennytm

2 Answers

3
votes

The digest you're using and the digest the sha2 crate is using are incompatible. That they have the same name is irrelevant; as far as the compiler is concerned, you're trying to conflate two entirely different crates.

The quickest way to tell would be to see if digest shows up more than once during compilation, or more than once in your Cargo.lock file. You can also verify this manually by looking at crate dependencies. sha2 0.3.0 lists digest 0.3 in its dependencies, and 0.3 is not compatible with 0.4.

You need to either downgrade your crate's dependency on digest, or upgrade your crate's version of sha2 to one that uses a newer version of digest.

0
votes

I just ran your code and it compiled fine with these dependency versions:

[dependencies]
sha2 = "0.4.2"
digest = "0.4.0"

Try updating one or both.