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

Question

你好,我是悦创。

学员答疑:

在这里插入图片描述

问题代码:

// 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,
    );
}

报错:

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

#[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,
    );
}

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 感知机学习是一种基本的机器学习算法,用于解决二分类问题,在 Rust 中,可以使用如下代码来实现:fn perceptron (weights: &[f64], input: &[f64]) -> f64 { let mut sum = 0.0; for i in 0..weights.len() { sum += weights[i] * input[i]; } if sum > 0.0 { 1.0 } else { -1.0 } } ### 回答2: 感知机学习方法是一种简单而有效的二分类算法,可以用于对给定的数据进行分类。以下是使用Rust编写的感知机学习方法的示例代码: ```rust use rand::{thread_rng, Rng}; struct Perceptron { weights: Vec<f64>, learning_rate: f64, } impl Perceptron { fn new(num_features: usize, learning_rate: f64) -> Self { let mut rng = thread_rng(); let weights: Vec<f64> = (0..num_features).map(|_| rng.gen_range(-1.0..1.0)).collect(); Perceptron { weights, learning_rate, } } fn activate(&self, features: &Vec<f64>) -> f64 { let weighted_sum: f64 = features.iter().zip(&self.weights) .map(|(x, w)| x * w) .sum(); if weighted_sum >= 0.0 { 1.0 } else { -1.0 } } fn train(&mut self, features: &Vec<f64>, target: f64) { let prediction = self.activate(features); let error = target - prediction; for (weight, feature) in self.weights.iter_mut().zip(features.iter()) { *weight += self.learning_rate * error * feature; } } } fn main() { let training_set = vec![ (vec![0.0, 0.0], -1.0), (vec![0.0, 1.0], -1.0), (vec![1.0, 0.0], -1.0), (vec![1.0, 1.0], 1.0), ]; let mut perceptron = Perceptron::new(2, 0.1); for _ in 0..100 { for (features, target) in &training_set { perceptron.train(features, *target); } } let test_data = vec![ vec![0.0, 0.0], vec![0.0, 1.0], vec![1.0, 0.0], vec![1.0, 1.0], ]; for features in &test_data { let prediction = perceptron.activate(features); println!("Input: {:?} - Prediction: {}", features, prediction); } } ``` 在这段代码中,我们定义了一个`Perceptron`结构体,它包含了权重向量和学习率。`activate`函数用于计算加权和并将其经过阈值函数进行分类。`train`函数根据误差调整权重向量。在`main`函数中,我们定义了一个训练集和测试集,并使用感知机算法对训练集进行训练。接着对测试集进行分类预测并输出结果。 请注意,本示例代码可能不是最优的实现方式,但足够演示感知机学习方法的基本原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI悦创|编程1v1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值