通过Rust编译时出现的错误来学习Rust
1. mismatched types - arguments to this function are incorrect
error[E0308]: mismatched types
--> xxxxxxx/src/keypair.rs:38:65
|
38 | let pubkey = PubKey::from(&ctx.serialize_pubkey(&pk, false)[1..]);
| ---------------- ^^^ expected struct `Point`, found enum `Result`
| |
| arguments to this function are incorrect
|
= note: expected reference `&Point`
found reference `&Result<Point, sm2::error::Sm2Error>`
参数类型是结构体,传入了枚举。建议用match处理pk
2. cannot index into a value of type Result<Vec<u8>, error::Error>
error[E0608]: cannot index into a value of type `Result<Vec<u8>, sm2::error::Sm2Error>`
--> xxxxxx/src/keypair.rs:40:52
|
40 | let pubkey = PubKey::from(&ctx.serialize_pubkey(&o, false)[1..]);
|
ctx.serialize_pubkey的返回值是枚举,PubKey::from的参数是结构体,无法解析枚举中的值。建议用match处理ctx.serialize_pubkey的返回值
3. match
arms have incompatible types
error[E0308]: `match` arms have incompatible types
--> xxxxx/src/keypair.rs:46:41
|
41 | / match ctx.serialize_pubkey(&o, false) {
42 | | Ok(pkb) => {
43 | | let pubkey = PubKey::from(&pkb[1..]);
44 | | KeyPair { privkey, pubkey }
| | --------------------------- this is found to be of type `keypair::KeyPair`
45 | | }
46 | | Err(e) => { e }
| | ^ expected struct `keypair::KeyPair`, found enum `error::Error`
47 | | }
| |_________________________- `match` arms have incompatible types
match 不能处理不兼容的类型,建议使用 let pkb = ctx.serialize_pubkey(&o, false);match pkb {}
4. no method named crypt_hash
found for reference &H512
in the current scope
error[E0599]: no method named `crypt_hash` found for reference `&H512` in the current scope
--> xxxxxx/src/keypair.rs:10:23
|
10 | H160::from(pubkey.crypt_hash())
| ^^^^^^^^^^ method not found in `&H512`
因为不同版本的特征不兼容 rustc 会报这个错误。要解决它,在新版本中将 Hashable 声明为依赖项,它将起作用。还有一种可能H512确实没有实现crypt_hash()。
5. unresolved import crate::crypto::Signature
error[E0432]: unresolved import `crate::crypto::Signature`
--> xxxxxxx/src/lib.rs:4:5
|
4 | use crate::crypto::Signature;
| ^^^^^^^^^^^^^^^^^^^^^^^^ no `Signature` in the root
error[E0432]: unresolved import `crate::crypto::Signature`
原因1:使用命令 rustc --explain E0432,可得到解释和解决办法。
原因2:因为有三个mod都实现了Signature,单独引入导致rustup无法识别,需要在Cargo.toml文件中,指定默认的实现,e.g.
[features]
default = ["sm3hash"]