简体   繁体   English

从 Rust 中的结构实例引用值时的范围问题

[英]Scoping Issues when referring value from Struct Instance in Rust

So I have this program and everything is working just as I want it to.所以我有了这个程序,一切都按照我的意愿工作。 The only problem is "student1.name" at the bottom.唯一的问题是底部的“student1.name”。 I wanted to replace "self.name" with "student1.name", but i'm having Scoping Issues.我想用“student1.name”替换“self.name”,但我遇到了范围问题。 "self.name" was working perfectly fine. “self.name”工作得很好。 Here is my code:这是我的代码:

 fn main() {
    let student1 = IOT_student {
        name: String::from("Husayn Abbas"),
        age: 13,
        education: String::from("O Levels"),
    };

    let instructor1 = IOT_instructor {
        name: String::from("Imran Ali"),
        age: 25,
    };

    println!("{}", student1.ask_Questions());
    println!("{}", instructor1.ask_Questions());
}

trait Questions {
    fn ask_Questions(&self) -> String;
}

struct IOT_student {
    name: String,
    age: i8,
    education: String,
}

struct IOT_instructor {
    name: String,
    age: i8,
}

impl Questions for IOT_student {
    fn ask_Questions(&self) -> String {
        return format!("Zoom session will be LIVE, Zoom recording will not be available. Quarter 2 studio recorded videos are available on Portal.");
    }
}

impl Questions for IOT_instructor {
    fn ask_Questions(&self) -> String {
        return format!("{} In case of any issue email to education@piaic.org", student1::name);
    }
}

And here is my output:这是我的 output:

Compiling IOT_Assignment_2 v0.1.0 (/home/memelord/Documents/PIAIC Quarter 2 IOT Assignments/IOT_Assignment_2)
error[E0425]: cannot find value `student1` in this scope
  --> src/main.rs:40:80
   |
40 |         return format!("{} In case of any issue email to education@piaic.org", student1.name);
   |                                                                                ^^^^^^^^ not found in this scope

warning: type `IOT_student` should have an upper camel case name
  --> src/main.rs:21:8
   |
21 | struct IOT_student {
   |        ^^^^^^^^^^^ help: convert the identifier to upper camel case: `IotStudent`
   |
   = note: `#[warn(non_camel_case_types)]` on by default

warning: type `IOT_instructor` should have an upper camel case name
  --> src/main.rs:27:8
   |
27 | struct IOT_instructor {
   |        ^^^^^^^^^^^^^^ help: convert the identifier to upper camel case: `IotInstructor`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.
error: could not compile `IOT_Assignment_2`.

To learn more, run the command again with --verbose.

Have any ideas why this is happening (i'm a Rust beginner so please try to make your explanation simple)?有任何想法为什么会发生这种情况(我是 Rust 初学者,所以请尽量让你的解释简单)?

The trait implementation methods are in completely different universe than the function calling them.特征实现方法与调用它们的 function 完全不同。

If you want to be able to use the name of a student in the call, you have to add an argument to the function.如果您希望能够在调用中使用学生的姓名,则必须向 function 添加一个参数。 Example:例子:

impl Questions for IOT_instructor {
    fn ask_Questions(&self, student: &IOT_student) -> String {
        return format!("{} In case of any issue email to education@piaic.org", student.name);
    }
}

Now call like:现在调用如下:

println!("{}", instructor1.ask_Questions(&student1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM