Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Rust中的结构实例引用值时的范围问题_Rust_Rust Cargo - Fatal编程技术网

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

从Rust中的结构实例引用值时的范围问题,rust,rust-cargo,Rust,Rust Cargo,所以我有这个计划,一切都按照我的要求进行。唯一的问题是底部的“student1.name”。我想用“student1.name”替换“self.name”,但我遇到了范围问题。“self.name”很好用。这是我的密码: fn main() { let student1 = IOT_student { name: String::from("Husayn Abbas"), age: 13, education: Str

所以我有这个计划,一切都按照我的要求进行。唯一的问题是底部的“student1.name”。我想用“student1.name”替换“self.name”,但我遇到了范围问题。“self.name”很好用。这是我的密码:

 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);
    }
}
这是我的输出:

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.

你知道为什么会发生这种情况吗(我是一个铁锈初学者,所以请尽量简化你的解释)?

trait实现方法与调用它们的函数完全不同

如果你想在调用中使用学生的名字,你必须在函数中添加一个参数。例如:

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);
    }
}
现在打电话给我:

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

student1在一个完全独立的、不相关的函数中定义时,为什么您希望能够引用它呢?我尝试将所有代码都放在fn main()中,但它给了我一个“无法在fn项中捕获动态环境”错误。我真的不知道如何在这种情况下使用闭包。如果我为“ask_Questions”函数添加另一个参数,我不能像现在这样调用“student1.ask_Questions()”。我希望这个函数有一个默认的实现,但据我所知,这是我唯一能做到的。与其问一些关于基本概念的问题,我建议你读一读这本书,这是我从中学到的。唯一的问题是,对于像我这样英语水平平平、自学计算机科学的人来说,真正理解他们在说什么是相当烦人的。我的意思是说,生锈是一种硬语言。在某些方面比C更难。在这一点上,我对这本铁锈书越来越讨厌了,于是我又买了一本铁锈书,希望它会更好。我以前做过Python和Java,也经常使用它们的文档,但因为Rust的级别很低,所以对我来说几乎没有任何意义。