Structure initialization quandry

⚓ Rust    📅 2025-09-23    👤 surdeus    👁️ 8      

surdeus

Warning

This post was published 39 days ago. The information described in this article may have changed.

I have a struct that contains a field I can't figure out how to initialize when the struct is created. I can initialize it later, but not when it is initialize instantiated. Its value depends on other fields. At least that is my understanding. Maybe there is a trick?

pub struct Lexer<'input> {                                                      
    reader: &'input mut BufReader<File>,                                        
    scaneol: bool,                                                              
    lineno: u32,                                                                
    chars: Peekable<Chars<'input>>,                                             
    first_column: usize,                                                        
    current_column: usize,                                                      
    needline: bool,                                                             
    linebuffer: String,                                                         
}                                                                               
                                                                                
impl<'input> Lexer<'input> {                                                    
    pub fn new(reader: &mut BufReader<File>) -> Self {                          
        let mut this = Lexer { reader: reader, scaneol: false, lineno: 1,       
                               linebuffer: String::new(),                       
                               chars: ,                                         
                               first_column: 0, current_column: 0,              
                               needline: false };                               
        let status = this.reader.read_line(&mut this.linebuffer).unwrap();      
        this.chars = this.linebuffer.chars().peekable();                        
        this                                                                    
    }                                                                           
}                                                                               

2 posts - 2 participants

Read full topic

🏷️ Rust_feed