Rust学习记录 -> 关于Crates.io的问题


前言

最近我在使用rust语言编写一个商场后端demo时,由于需要与mysql进行交互以及序列化等操作,所以通过crates.io下载了许多外部依赖包,在这个过程中我遇到了由版本更迭,镜像网站之间版本不一所带来的问题,具体如下文所述


问题描述与解析

1.版本更迭带来的依赖包适配问题

部分报错代码如下:

error[E0308]: mismatched types
   --> /home/hadoop/.cargo/registry/src/github.com-1ecc6299db9ec823/lexical-core-0.4.6/src/atof/algorithm/bigcomp.rs:243:55
    |
243 |     let nlz = den.leading_zeros().wrapping_sub(wlz) & (u32::BITS - 1);
    |                                                       ^^^^^^^^^^^^^^^ expected `usize`, found `u32`

error[E0277]: no implementation for `usize & u32`
   --> /home/hadoop/.cargo/registry/src/github.com-1ecc6299db9ec823/lexical-core-0.4.6/src/atof/algorithm/bigcomp.rs:243:53
    |
243 |     let nlz = den.leading_zeros().wrapping_sub(wlz) & (u32::BITS - 1);
    |                                                     ^ no implementation for `usize & u32`
    |
    = help: the trait `BitAnd<u32>` is not implemented for `usize`

error[E0308]: mismatched types
   --> /home/hadoop/.cargo/registry/src/github.com-1ecc6299db9ec823/lexical-core-0.4.6/src/atof/algorithm/bigcomp.rs:261:40
    |
261 |         let (q, r) = shift.ceil_divmod(Limb::BITS);
    |                                        ^^^^^^^^^^ expected `usize`, found `u32`
    |

可以发现报错的外部包是 lexical_core-0.4.6 ,起初我对包中的代码进行修改,发现并无效果,于是开始查询Crates.io官方文档。在查询了 lexical-core包的具体文档以及版本更新情况后,我得知该外部包最新版本已经更新到了0.8.2版本,从镜像网站下载的包已经严重滞后。

以上是出现问题的一个原因,其次,笔者在查询Crates.io官方文档前,还对代码进行了修改,例如将 u32:BITS - 1 修改为 usize:BITS - 1 ,但是报错并未发生变化,所以我还对cargo版本进行查询,对笔者版本(1.55.0)的官方文档进行阅读,对BITS有如下发现:

pub const BITS: u32

The size of this integer type in bits.
Examples

assert_eq!(u32::BITS, 32);
pub const BITS: u32

The size of this integer type in bits.
Examples

assert_eq!(usize::BITS, 64);

所以修改代码后才会无效。

2. openssl

在更换Crates.io镜像源地址后再次运行代码,出现了:Could not find directory of OpenSSL installation 的报错,而我的ubuntu20.04已经安装了openssl最新版本,最后在这里发现了问题的解决方案:

Mac OSX / Homebrew
If you have Homebrew installed:
$ brew install openssl@1.1

Debian / Ubuntu
Using the apt package manager:
$ apt install pkg-config libssl-dev

Fedora / RHEL / CentOS
And using yum:
$ pkg-config openssl-devel

Or with the newer dnf package manager:
$ dnf install pkg-config openssl-devel

转自https://ma.ttias.be/could-not-find-directory-of-openssl-installation/

至此,暂时遇到的问题就全部解决。


总结

首先,由于Rust语言相较于python等语言并不是那么主流,所以在一般的网站及博客中难以找到报错的解决方案,这让我不得不开始阅读官方提供的文档注释,既是一种挑战也是一种成长,习惯后还有些依赖于官方文档。
其次,由于我在安装rust编译环境时使用了上交大源,而在更新Crates.io镜像源时只是简单的更换了中科大源,并未考虑不同镜像源之间的适配问题,比如在更换镜像源过程中,笔者发现对于上文提到的 lexical-core 外部包,不同镜像源网站所提供的版本就是不一致的,所以才导致了生产过程中严重的报错。

最后,经过此次经历,笔者多有成长,也对一些方法的使用更加熟练。至此感谢您的阅读,如有疑问或是指正,还请你不吝赐教。

猜你喜欢

转载自blog.csdn.net/weixin_45704680/article/details/121260078
今日推荐