Hasher trait进行配置。let leaf_values = ["a", "b", "c", "d", "e", "f"];
let leaves: Vec<[u8; 32]> = leaf_values
.iter()
.map(|x| Sha256::hash(x.as_bytes()))
.collect();
let merkle_tree = MerkleTree::<Sha256>::from_leaves(&leaves);
let indices_to_prove = vec![3, 4];
let leaves_to_prove = leaves.get(3..5).ok_or("can't get leaves to prove")?;
let merkle_proof = merkle_tree.proof(&indices_to_prove);
let merkle_root = merkle_tree.root().ok_or("couldn't get the merkle root")?;
// Serialize proof to pass it to the client
let proof_bytes = merkle_proof.to_bytes();
// Parse proof back on the client
let proof = MerkleProof::<Sha256>::try_from(proof_bytes)?;
assert!(proof.verify(merkle_root, &indices_to_prove, leaves_to_prove, leaves.len()));
User结构体的代码示例:#[derive(Debug)]
struct User {
id: String,
email: String,
first_name: Option<String>,
last_name: Option<String>
}
struct UserBuilder {
id: String,
email: String,
first_name: Option<String>,
last_name: Option<String>
}
impl UserBuilder {
fn new(id: impl Into<String>, email: impl Into<String>) -> Self {
Self {
id: id.into(),
email: email.into(),
first_name: None,
last_name: None,
}
}
fn first_name(mut self, first_name: impl Into<String>) -> Self {
self.first_name = Some(first_name.into());
self
}
fn last_name(mut self, last_name: impl Into<String>) -> Self {
self.last_name = Some(last_name.into());
self
}
fn build(self) -> User {
let Self { id, email, first_name, last_name } = self;
User { id, email, first_name, last_name }
}
}
impl User {
fn builder(id: impl Into<String>, email: impl Into<String>) -> UserBuilder {
UserBuilder::new(id, email)
}
}
fn main() {
let greyblake = User::builder("13","greyblake@example.com")
.first_name("Sergey")
.build();
dbg!(greyblake);
}
UserBuilder来创建User结构体;new方法,可选参数通过setter来设置;build函数返回完整的结构体实例;sila - 读作[shila],巴利语中代表「道德」;
> help
list List the terminal names.
pin <term1> <term2> Pin one or multiple terminals separated by space. Following commands will be run on top of pinned ones.
unpin [term1] Unpin all terminals if no argument is provided or the specific ones.
count Count the number of terminals.
exit Close the application.
From 日报小组 odd-cat
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。