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,
    );
}
相关推荐
一匹电信狗16 分钟前
【牛客CM11】链表分割
c语言·开发语言·数据结构·c++·算法·leetcode·stl
2501_9389639621 分钟前
解析 Lua 虚拟机整数与浮解析 Lua 虚拟机整数与浮点数处理:类型转换与运算精度控制
开发语言·lua
不染尘.23 分钟前
图的邻接矩阵实现以及遍历
开发语言·数据结构·vscode·算法·深度优先
国服第二切图仔26 分钟前
Rust开发之Trait作为参数与返回值使用
开发语言·后端·rust
山峰哥33 分钟前
KingbaseES 表空间与模式优化策略深度研究报告
开发语言·数据结构·数据库·oracle·深度优先
胖哥真不错42 分钟前
Python基于PyTorch实现多输入多输出进行BP神经网络回归预测项目实战
pytorch·python·毕业设计·论文·毕设·多输入多输出·bp神经网络回归预测
yong999043 分钟前
C++语法—类的声明和定义
开发语言·c++·算法
狂奔的sherry1 小时前
构造/析构/赋值运算理解
开发语言·c++
大佬,救命!!!1 小时前
C++多线程运行整理
开发语言·c++·算法·学习笔记·多线程·新手练习
红尘散仙1 小时前
TRNovel王者归来:让小说阅读"声"临其境的终端神器
前端·rust·ui kit