变量绑定默认是不可变的(immutable),但加上 mut 修饰语后变量就可以改变。 ``
Common traits
Eq, PartialEq, Ord, PartialOrdClone, 用来从 &T 创建副本 T。Copy,使类型具有 “复制语义”(copy semantics)而非 “移动语义”(move semantics)。Hash,从 &T 计算哈希值(hash)。Default, 创建数据类型的一个空实例。Debug,使用 {:?} formatter 来格式化一个值。_ underscore type: 让编译器去推导类型
b'a' means byte string literal, this string is treated as a byte sequence.
i32 != &i32
let var: i32 = 5;
// assert_eq!(var * 8, 40); bad code! type of var is i32; type of 40 is &i32
assert_eq!(&var * 8, 40);
? vs unwrap()
? 解包(unwrap)有效值或返回错误值,并将它们传播(propagate)给调用函数。unwrap() 解包有效值或 panic!match
{
match caller {
_admin if caller == self.admin => {
self.badge_contract_options = Some((contract, badge_id));
Ok(())
}
_ => Err(Error::BadOrigin),
}
}
// equivalent to
{
if caller != self.admin {
return Err(Error::BadOrigin);
}
self.badge_contract_options = Some((contract, badge_id));
Ok(())
}