How do I get arround compiler "stupidity"?

⚓ Rust    📅 2025-09-08    👤 surdeus    👁️ 2      

surdeus

I have this method:

    pub fn RunAllTrains(&mut self, printer: &mut Printer) {                     
        // Get the initial car counts.                                          
        self.GetIndustryCarCounts();                                            
        // Reset the switch lists.                                              
        self.switchList.ResetSwitchList();                                      
        // Flag that we were called.                                            
        self.ranAllTrains += 1;                                                 
        // Display our banner.                                                  
        println!("{}",self.SystemName());                                       
        // First runn all of the box moves (yard locals).                       
        self.RunBoxMoves(printer);                                              
        let boxMove = false;                                                    
        // For every train...                                                   
        for Tx in 1..self.maxTrain+1 {                                          
            if !self.trains.contains_key(&Tx) {continue;}                       
            if self.trains[&Tx].Type() == TrainType::Manifest ||                
               self.trains[&Tx].Type() == TrainType::Wayfreight {               
                if self.trains[&Tx].Shift() == self.shiftNumber {               
                    self.InternalRunOneTrain(Tx,boxMove,printer);               
                }                                                               
            }                                                                   
        }                                                                       
    }

That I am coding here very ineffiencently. self.trains is a very sparse vector, so I am coding it as a HashMap with usize keys. The rust compiler won't let me iterate over the keys, because that requires and immutable borrow (self.trains.keys()) and later I am making a mutable borrow: I know that the map's keys are NOT going to change in the InternalRunOneTrain method, but the compiler is being very paranoid. What sort of syntatic sugar should I use to make the compiler chillout? I would like to do something like for Tx in self.trains.keys().

14 posts - 4 participants

Read full topic

🏷️ Rust_feed