通过Rust编译时出现的错误来学习Rust

本文介绍了在使用Rust编程时遇到的五种常见编译错误,包括类型不匹配、无法对Result枚举解构、match语句中类型不兼容、方法未找到以及未解析的导入。解决方案涉及使用match处理错误、确保类型匹配以及正确导入和使用库函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过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"]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值