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,
    );
}
相关推荐
捕鲸叉28 分钟前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer33 分钟前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq35 分钟前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
阡之尘埃1 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
记录成长java2 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
前端青山2 小时前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js
hikktn2 小时前
如何在 Rust 中实现内存安全:与 C/C++ 的对比分析
c语言·安全·rust
睡觉谁叫~~~2 小时前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
音徽编程2 小时前
Rust异步运行时框架tokio保姆级教程
开发语言·网络·rust
观音山保我别报错2 小时前
C语言扫雷小游戏
c语言·开发语言·算法