Rust代码答疑报错|Python一对一辅导答疑

Question

你好,我是悦创。

学员答疑:

问题代码:

rust 复制代码
// You can bring module paths into scopes and provide new names for them with
// the `use` and `as` keywords.

#[allow(dead_code)]
mod delicious_snacks {
    // TODO: Add the following two `use` statements after fixing them.
    use self::fruits::PEAR as fruits;
    use self::veggies::CUCUMBER as veggies;

    pub mod fruits {
        pub const PEAR: &str = "Pear";
        pub const APPLE: &str = "Apple";
    }

    pub mod veggies {
        pub const CUCUMBER: &str = "Cucumber";
        pub const CARROT: &str = "Carrot";
    }
}

fn main() {
    println!(
        "favorite snacks: {} and {}",

        delicious_snacks::fruit,
        delicious_snacks::veggie,
    );
}

报错:

bash 复制代码
D:\LagerA\rust\a-ex\rustlings git:[master]
rustlings run modules2
error[E0425]: cannot find value `fruit` in module `delicious_snacks`
  --> exercises/10_modules/modules2.rs:25:27
   |
11 |         pub const PEAR: &str = "Pear";
   |         ------------------------------ similarly named constant `fruits` defined here
...
25 |         delicious_snacks::fruit,
   |                           ^^^^^ help: a constant with a similar name exists: `fruits`

error[E0425]: cannot find value `veggie` in module `delicious_snacks`
  --> exercises/10_modules/modules2.rs:26:27
   |
16 |         pub const CUCUMBER: &str = "Cucumber";
   |         -------------------------------------- similarly named constant `veggies` defined here
...
26 |         delicious_snacks::veggie,
   |                           ^^^^^^ help: a constant with a similar name exists: `veggies`

warning: unused import: `self::fruits::PEAR as fruits`
 --> exercises/10_modules/modules2.rs:7:9
  |
7 |     use self::fruits::PEAR as fruits;
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `self::veggies::CUCUMBER as veggies`
 --> exercises/10_modules/modules2.rs:8:9
  |
8 |     use self::veggies::CUCUMBER as veggies; 
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  

For more information about this error, try `rustc --explain E0425`.
error: could not compile `exercises` (bin "modul
es2") due to 2 previous errors; 2 warnings emitted

Error: Ran exercises/10_modules/modules2.rs with errors

Solution

rust 复制代码
#[allow(dead_code)]
mod delicious_snacks {
    // 使用 `use` 语句将常量导入到模块的顶层作用域
    pub use self::fruits::PEAR as fruit;
    pub use self::veggies::CUCUMBER as veggie;

    pub mod fruits {
        pub const PEAR: &str = "Pear";
        pub const APPLE: &str = "Apple";
    }

    pub mod veggies {
        pub const CUCUMBER: &str = "Cucumber";
        pub const CARROT: &str = "Carrot";
    }
}

fn main() {
    println!(
        "favorite snacks: {} and {}",
        delicious_snacks::fruit,
        delicious_snacks::veggie,
    );
}
相关推荐
兮动人39 分钟前
C语言之指针入门
c语言·开发语言·c语言之指针入门
ada7_1 小时前
LeetCode(python)78.子集
开发语言·数据结构·python·算法·leetcode·职场和发展
我送炭你添花1 小时前
Pelco KBD300A 模拟器:06+5.串口实现的逻辑优化、配置管理与协议完善(二次迭代)
python·运维开发
databook1 小时前
前注意加工:让你的图表抓住读者的眼球
python·数据分析·数据可视化
知行学思1 小时前
Python配置管理完全指南:从dotenv到pydantic_settings
数据库·python·fastapi·环境变量·配置管理·pydantic·dotenv
nbsaas-boot2 小时前
Go 项目中如何正确升级第三方依赖(Go Modules 实战指南)
开发语言·后端·golang
wadesir2 小时前
C++基本数据类型详解(零基础掌握C++核心数据类型)
java·开发语言·c++
skywalk81633 小时前
wow文件处理trinitycore的文件处理
开发语言·游戏
一路往蓝-Anbo4 小时前
STM32单线串口通讯实战(五):RTOS架构 —— 线程安全与零拷贝设计
c语言·开发语言·stm32·单片机·嵌入式硬件·观察者模式·链表
leiming64 小时前
c++ map容器
开发语言·c++·算法