rustbase58

Converting an array, slice or vector to base58 encoding WITH check


I'm writing a code that converts an array/slice/vector to a B58-encoded string with the check. Here is the relevant code excerpt:

use bs58;
i = b"Hello"; // or
i = [0,1,2,3]; // or
i = vec![0,1,2,3];
let e = bs58::encode(i).with_check().into_string();

No matter what type I supply to bs58::encode(), it errors out saying that method with_check() not found. Coming from Python, it's really frustrating because I have to spend hours debugging simple stuff that should just work.


Solution

  • If you look in the API documentation for bs58::encode, you see that it returns a EncodeBuilder.

    Looking at the documentation for that, you see that there is a with_check method but it has a note attached to it:

    This is supported on crate feature check only.

    Rust supports crates defining optional features - these features typically have extra dependancies that are not needed in all cases, so they are off by default.

    You can enable the extra features in your Cargo.toml file, like this:

    [dependancies]
    bs58 = { version="0.4.0", features=["check"] }
    

    See also the Features section of the Cargo book.