首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在rust的main函数中使用impl函数?

问如何在rust的main函数中使用impl函数?
EN

Stack Overflow用户
提问于 2021-11-03 16:13:34
回答 1查看 52关注 0票数 0

我想在我正在构建的rust程序的main函数中使用一个函数来帮助我学习rust并提出一个错误:rust值是一个仅在具有self参数rust(E0424)的方法中可用的关键字。我可以在我的代码中修复什么才不会发生此错误?

代码语言:javascript
复制
pub use crate::user_account::user_account;
use rand::Rng;

#[allow(dead_code)]
pub trait UserInfo {
    fn user_info(&mut self);
    fn acc_no(&mut self);
    fn yes(self);
    fn bank_new_user(self);
}
pub struct NewUser {
    age: String,
    new_user: String,
    account: String,
    account_number: i32,
    routing_number: i32,
    select: String,
}
impl UserInfo for NewUser {
    fn user_info(&mut self) {
        self.age = String::new();
        self.new_user = String::new();
        println!("What is your name?");
        print!("Name: ");
        std::io::stdin().read_line(&mut self.new_user);
        println!(" ");
        println!("Hello {}, What is your age? ", self.new_user);
        std::io::stdin().read_line(&mut self.age);
        let age2: String = self.age.trim().into();

    }

    fn acc_no(&mut self) {
        println!(
            "We will generate a new account number \
               and routing number for you."
        );
        self.account_number = rand::thread_rng().gen_range(10000000..99999999);
        println!("Your account number is {}", self.account_number);
        self.routing_number = rand::thread_rng().gen_range(10000000..99999999);
        println!("Your account routing number is {}", self.routing_number);
    }
    fn yes(self) {
        NewUser::user_info(&mut self);
        NewUser::acc_no(&mut self);
    }
    //function I want to use in main.
    fn bank_new_user(self) {
        self.account = String::new();
        println!("Would you like to make a new account with us today?");
        loop {
            println!(
                " yes: continue to application, no: continue browsing , \
        or exit: to exit"
            );
            self.account.clear();
            std::io::stdin()
                .read_line(&mut self.account)
                .expect("please type yes, no or exit.");
            let account = self.account.trim();
            match account {
                "yes" => {
                    self.yes();
                    break;
                }

                "no" => {
                    println!("You do not need an account to continue browsing.");
                    println!("Have a wonderful day and thank you for considering Mars Banking!");
                    break;
                }
                "exit" => {
                    println!(
                        "Thank you for choosing Mars Banking for your banking needs!\
                Have a wonderful day!"
                    );
                    break;
                }
                _ => {
                    println!("Error! Enter yes, no, or exit.")
                }
            }
        }
    }
}

pub mod new_user;
mod settings;
mod user_account;
pub use crate::settings::settings;
pub use crate::user_account::user_account;
use new_user::NewUser;
use new_user::UserInfo;

fn main() {
    loop{
        let mut select = String::new();
            println!("Welcome to Mars Banking!");
            println!("What would you like to do today?");
            println!("Create a new account: 1\nLogin: 2\nSettings: 3\nExit: 4");
            select.clear();
            std::io::stdin().read_line(&mut select);
            let select = select.trim();
            match select {
                //Here is where the error happens.
                "1" => NewUser::bank_new_user(self),
                "2" => user_account(),
                "3" => settings(),
                "4" => break,
                _ => {}
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-03 17:11:10

此类构造函数的常规模式是不接受self参数的静态方法,如下所示:

代码语言:javascript
复制
impl NewUser {
    fn bank_new_user() {
        let mut new_user = NewUser { /* initialize the fields */ };
        // Edit or use new_user as necessary
    }
}

您可以在为Point定义的方法中看到此here的示例

代码语言:javascript
复制
struct Point {
    x: f64,
    y: f64,
}

// Implementation block, all `Point` associated functions & methods go in here
impl Point {
    // This is an "associated function" because this function is associated with
    // a particular type, that is, Point.
    //
    // Associated functions don't need to be called with an instance.
    // These functions are generally used like constructors.
    fn origin() -> Point {
        Point { x: 0.0, y: 0.0 }
    }

    // Another associated function, taking two arguments:
    fn new(x: f64, y: f64) -> Point {
        Point { x: x, y: y }
    }
}

注意niether origin和new是如何将self作为参数的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69828226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档