rust 建造者模式


#[derive(Debug,Serialize,Deserialize,Default)]
pub struct TableInfo{
    sheet_name:String,
    table_token:String,
    sheet_title:String,
}


impl TableInfo {
    #[warn(non_snake_case)]
    #[allow(dead_code)]
    pub fn new() -> Self{
        TableInfo{
            ..Default::default()
        }
    } 

    #[allow(dead_code)]
    pub fn set_name(mut self, sheet_name:&str) -> Self{
        self.sheet_name = sheet_name.to_string();
        self
    }

    #[warn(non_snake_case)]
    #[allow(dead_code)]
    pub fn set_table_token(mut self, table_token:&str) -> Self{
        self.table_token = table_token.to_string();
        self
    }

    #[warn(non_snake_case)]
    #[allow(dead_code)]
    pub fn sheet_title(mut self, sheet_title:&str) -> Self{
        self.sheet_title = sheet_title.to_string();
        self
    }

    #[allow(dead_code)]
    pub fn build(self) -> Self{
        self
    }
}

原文地址:https://www.cnblogs.com/Alpacapyer/p/14693226.html