为什么Rust找不到“js_sys”包?

3
我对 Rust 还不熟悉。我尝试使用 crate js_sys,其中包含 Math::log。按照 crate 网站告诉我的,我已经在 Cargo.toml 中包含了 js_sys = 0.3.48,并在 main.rs 中使用 use js_sys::Math::log;。然而,我收到了一个错误,提示 rust 找不到 crate。
复制步骤如下:
[package]
name = "sim"
version = "0.1.0"
authors = ["Excluded for privacy"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
js_sys = "0.3.48"
rand = "0.8.3"

在我的main.rs文件的顶部
// Luke Anglin and Tobi Solarin
use js_sys::Math::log;
use rand::prelude::*; // For the rng
const n: i32 = 1_000; // The number of trials

错误

error: no matching package named `js_sys` found
location searched: registry `https://github.com/rust-lang/crates.io-index`
perhaps you meant: js-sys
required by package `sim v0.1.0 (/Users/lukeanglin/Desktop/Probability/Project2/simulator/sim)`
1个回答

4

将 Cargo.toml 文件中的 js_sys 改为 js-sys,然后它就应该可以工作了。(如您发布的错误中所述,但很容易被忽略)


所以,这很有趣。我最初看到了这个,但当我执行我的 use 时,我还在 cargo.toml 中将其更改为 js-sys,但它没有起作用。显然,在 toml 中必须使用破折号,但在 use 中不需要。对我来说很困惑! - user11391572
1
创建的包名是 "js-sys",但由于标识符中不能使用 "-",所以在导入名称中将其替换为 "_". - kmdreko

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接