简体   繁体   中英

Unable to use rust crates

I'm new to rust. I'm following a getting started tutorial that imports the crate random-number but when running the code I'm getting the error can't find crate for 'random_number' . What am I doing wrong?

~/Cargo.toml :

[package]
name = "test"
version = "0.0.1"
edition = "2021"

[dependencies]
random-number = "0.1.8"

~/src/main.rs :

extern crate random_number;
use random_number::random;

fn main() {
    let num: i8 = random!(..);
    println!("{}", num);
}

rustc is not meant to be used directly. It is the compiler that can compile a .rs file, but it doesn't have any dependency manager attached to it. So if you decide to use rustc directly, you need to manage your dependencies manually.

cargo is the official tool to compile Rust projects. It internally uses rustc , but additionally manages the project's dependencies that are specified in Cargo.toml .

cargo build --release && ./target/release/<project_name>

or the short form:

cargo run --release

The version is off.You have to change the version to 0.1.0, not 0.0.1. If that doesn't work try running cargo clean. Once you complete it, closing and opening your IDE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM