简体   繁体   中英

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. I wanted to replace "self.name" with "student1.name", but i'm having Scoping Issues. "self.name" was working perfectly fine. 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:

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)?

The trait implementation methods are in completely different universe than the function calling them.

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. 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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