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,
    );
}
相关推荐
从零开始的代码生活_5 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸5 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
C^h5 小时前
python函数学习
人工智能·python·机器学习
Fanta丶6 小时前
4.Python set()集合、dict(字典、映射)、 数据容器的通用功能
python
决战灬6 小时前
langgraph之interrupt(理论篇)
人工智能·python·agent
GIS阵地6 小时前
QgsSingleBandPseudoColorRenderer 完整详解(QGIS 3.40.13 C++)
开发语言·前端·c++·qt·qgis
兜客互动6 小时前
2026年AI关键词拓展挖掘软件,高效助力内容创作精准获流
人工智能·python
weixin_538601976 小时前
智能体测开Day30pytest测试框架
python
MC皮蛋侠客6 小时前
uv 系列(七):CI/CD、Docker 与私有索引——生产级交付
python·ci/cd·docker·uv
邪神与厨二病6 小时前
牛客周赛 Round 153
python·算法