Rust入门问题: use of undeclared crate or module `rand`

按照官网学rust,程序地址在这里,

写个猜数字游戏 - Rust 程序设计语言 简体中文版

程序内容也很简单,

use std::io;
use rand::Rng;

fn main() {
    println!("Guess the number!");

    let secret_number = rand::thread_rng().gen_range(1..=100);

    println!("The secret number is: {secret_number}");

    println!("Please input your guess.");

    let mut guess = String::new();

    io::stdin()
        .read_line(&mut guess)
        .expect("Failed to read line");

    println!("You guessed: {guess}");
}

然后,程序运行时报错:

use of undeclared crate or module `rand`

原因:

运行程序找不到库,或者叫依赖(rust里有crates, package, module,看字面理解吧)

解决办法:

在cargo.toml中dependencies添加一条:

rand="0.8.5" ,如下,

[package]
name = "guessing_game"
version = "0.1.0"
edition = "2021"

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

[dependencies]
rand="0.8.5"

这里0.8.5是指版本,跟你的rust的版本有关。

本文结束。

相关推荐

  1. Rust入门

    2024-02-12 06:04:01       53 阅读
  2. Rust入门

    2024-02-12 06:04:01       32 阅读
  3. Rust入门问题: use of undeclared crate or module `rand`

    2024-02-12 06:04:01       58 阅读
  4. Rust入门2——随机数

    2024-02-12 06:04:01       52 阅读
  5. rust入门学习---所有权

    2024-02-12 06:04:01       56 阅读
  6. rust语言学习入门

    2024-02-12 06:04:01       42 阅读
  7. Rust编程入门教程

    2024-02-12 06:04:01       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-12 06:04:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-12 06:04:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-12 06:04:01       87 阅读
  4. Python语言-面向对象

    2024-02-12 06:04:01       97 阅读

热门阅读

  1. Pytorch 复习总结 1

    2024-02-12 06:04:01       43 阅读
  2. TCP/IP五层各层协议详解

    2024-02-12 06:04:01       46 阅读
  3. Chapter 8 - 9. Congestion Management in TCP Storage Networks

    2024-02-12 06:04:01       50 阅读
  4. 2024-02-11 叮当鸭-平台系统-第三次重构-目标确定

    2024-02-12 06:04:01       48 阅读
  5. vue3学习——封装菜单栏

    2024-02-12 06:04:01       46 阅读
  6. AtCoder Beginner Contest 340C - Divide and Divide

    2024-02-12 06:04:01       57 阅读
  7. Flask基础学习2

    2024-02-12 06:04:01       53 阅读
  8. 6. 尚硅谷大数据111门技术+42个项目

    2024-02-12 06:04:01       55 阅读