3

I'm new to rust. I'm trying to use the crate js_sys which contains a Math::log. I have included js_sys = 0.3.48 as the crate website tells me, and then use js_sys::Math::log; in main.rs. I get an error that rust cannot find the crate.

Steps to replicate:

In Cargo.toml

[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"

In the top of my 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

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)`
curiousdannii
  • 1,658
  • 1
  • 25
  • 40

1 Answers1

4

Change js_sys to js-sys in your Cargo.toml and it should work. (As stated in the error you posted but easily overlooked)

  • So, this is interesting. I initlaly saw that, but when i did my `use` I also wrote js-sys after changing it to that in the cargo.toml and it didnt work. Apparently, you have to write with the dash in the toml, but NOT in the use. Confusing to me! –  Mar 16 '21 at 23:10
  • 1
    the name of crate is "js-sys", however since "-"s are not valid in identifiers, they're replaced with "_" for the import name – kmdreko Mar 17 '21 at 04:12