Why can't Rust find the "js_sys" crate? - rust

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)`

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

Related

How to change the app name but not library name of a package?

In Rust, I created one package and now I want to change only output app name without changing package name.
Below is the content of Cargo.toml file:
[package]
authors = ["Rust exam"]
edition = "2021"
name = "rust-exam"
description = "Rebuilt for Scale"
version = "1.10.0"
license = "Apache-2.0"
[dependencies]
base64 = "0.12.3"
clap = "2.33.1"
serde = "1.0.132"
serde_json = "1.0.73"
serde_yaml = "0.8.23"
tempfile = "3.2.0"
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
When I input cargo build, it makes rust-exam and librust-exam.
I want to only change rust-exam, not change package name. How can I do that?
You can configure the app name like so:
[[bin]]
name = "rust-output"
path = "src/main.rs"

How to deal with unresolved import 'concrete' issues in RUST

I am new to RUST, and I am trying to use this concrete library: https://github.com/zama-ai/concrete/tree/master/concrete. I am trying to create a simple "Hello World" in RUST to see if concrete imports correctly. I followed the instructions in the aforementioned link.
Specifically, I:
Cloned the GitHub repo.
Cd into concrete folder (/concrete/concrete)
ran "cargo new play_with_fhe"
Updated the "Cargo.toml" file with the new member "play_with_me"
[workspace]
members = [
"concrete",
"concrete-npe",
"concrete-core",
"concrete-csprng",
"concrete-commons",
"concrete-tasks",
"concrete-boolean",
"play_with_fhe",
]
[profile.bench]
opt-level = 3
debug = true
lto="fat"
[patch.crates-io]
concrete = {path="concrete"}
concrete-npe = {path="concrete-npe"}
concrete-core = {path="concrete-core"}
concrete-csprng = {path="concrete-csprng"}
concrete-commons = {path="concrete-commons"}
concrete-boolean = {path= "concrete-boolean"}
play_with_fhe = {path= "play_with_fhe"}
Cd into "/concrete/concrete/play_with_fhe" and updated the "Cargo.toml" file with
[package]
name = "play_with_fhe"
version = "0.1.11"
authors = ["FHE Curious"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
concrete = "^0.1.11"
Cd into /concrete/concrete/play_with_fhe_/src and created a main.rs file running a simple code:
use concrete::*;
fn main() {
println!("Hello, world!");
}
When I try compiling it with rustc main.rs, I get told that:
error[E0432]: unresolved import `concrete`
--> main.rs:2:5
|
2 | use concrete::*;
| ^^^^^^^^ maybe a missing crate `concrete`?
error: aborting due to previous error
For more information about this error, try `rustc --explain E0432`.
How can I address this issue? Any advice would be appreciated.
Since you're trying to create your own hello world project, you don't need to clone the repository. You just need to create a project, include concrete as a dependency, and then import it. Those instructions are on the concrete page (as Stargateur notes):
% cargo new play_with_fhe
% cd play_with_fhe
Add concrete to your dependencies in Cargo.toml:
[package]
name = "play_with_fhe"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
concrete = "^0.1" # <--- This is the only change you make. The rest is template.
Then add use cargo::* to the top of your main.rs, and build it:
% cargo build
This will install everything. For more on Cargo, see Dependencies in Rust By Example.
Note that this package likely won't build correctly unless you're on an x86 architecture. For example, it won't run on an Apple M1 without Rosetta2.

include toml file from another toml

I am trying to include a second toml file from Cargo.toml file.
I don't known how to do that and i don't known do this is possible.
I am trying this:
Cargo.toml:
[package]
name = "toml"
authors = "TANDEX"
version = "0.0.0"
include = ["libs.toml"]
libs.toml:
[dependencies]
termion = "0.9.8"
and this:
Cargo.toml:
include = ["libs.toml"]
[package]
name = "toml"
authors = ["TANDEX"]
version = "0.0.0"
libs.toml:
[dependencies]
termion = "0.9.8"
Both of them don't works.
For information I want to automatically generate one file by script and the `Cargo.toml` leave normal.

Rust: Cargo.toml error: expected item, found `[`, when trying to include nalgebra

[package]
name = "my package"
version = "0.1.0"
authors = ["me"]
edition = "2018"
[dependencies]
nalgebra = "0.18.1"
I tried to compile rust with above Cargo.toml but it gave me following error:
error: expected item, found `[`
--> Cargo.toml:1:1
|
1 | [package]
| ^ expected item
error: aborting due to previous error
I can fix this if I remove nalgebra = "0.18.1", but I can't use nalgebra package so it doesn't help me.
I think there are multiple issues with your Cargo.toml
This is not allowed:
name = "my package"
Suggestion:
name = "my-package"
There is also missing parts:
error during execution of `cargo metadata`: error: failed to parse manifest at `test/Cargo.toml`
Caused by:
no targets specified in the manifest
either src/lib.rs, src/main.rs, a [lib] section, or [[bin]] section must be present
Could you post the entire file here?
A minimum viable setup:
[package]
name = "my-package"
version = "0.1.0"
authors = ["me"]
edition = "2018"
[[bin]]
name = "radkilla"
path = "src/main.rs"
doc = false
[dependencies]
nalgebra = "0.18.1"
src/main.rs
fn main() {}
Running fmt and build:
➜ test cargo fmt ; cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.11s
The error disappeared after closing the cargo.toml file which was opened on the visual studio.

How to conditionally compile in Rust [duplicate]

I've followed quite a bit of the documentation and tried to reuse an example, but I can't get my code to work.
My Cargo.toml looks like this:
[package]
name = "Blahblah"
version = "0.3.0"
authors = ["ergh <derngummit#ahwell.com"]
[dependencies]
[[bin]]
name = "target"
path = "src/main.rs"
[features]
default=["mmap_enabled"]
no_mmap=[]
mmap_enabled=[]
I'd like to test my code locally with a different buffer origin than mmap based on what feature configuration I pass to the cargo build command. I have this in my code:
if cfg!(mmap_enabled) {
println!("mmap_enabled bro!");
...
}
if cfg!(no_mmap) {
println!("now it's not");
...
}
The compiler doesn't see the code in either of the if statement bodies, so I know that both of the cfg! statements are evaluating to false. Why?
I've read Conditional compilation in Rust 0.10? and I know it's not an exact duplicate because I'm looking for a functioning example.
The correct way to test for a feature is feature = "name", as you can see in the documentation you linked if you scroll a bit:
As for how to enable or disable these switches, if you’re using Cargo,
they get set in the [features] section of your Cargo.toml:
[features]
# no features by default
default = []
# Add feature "foo" here, then you can use it.
# Our "foo" feature depends on nothing else.
foo = []
When you do this, Cargo passes along a flag to rustc:
--cfg feature="${feature_name}"
The sum of these cfg flags will determine which ones get activated,
and therefore, which code gets compiled. Let’s take this code:
#[cfg(feature = "foo")]
mod foo {
}
In your case using the cfg! macro, this would map to cfg!(feature = "foo").

Resources