According to electrochemical theory of rusting the impure iron surface behaves like small electrochemical cell in the presence of water containing dissolved oxygen or carbon dioxide. In this cell pure iron acts as anode and impure iron surface acts as cathode. Moisture having dissolved CO2 or O2 acts as an electrolyte. The reactions are given below.
The Fe2+ ions are further oxidised by atmospheric oxygen to Fe3+ ions, which comes out in the form of hydrated ferric oxide (rust).