Corrosion is possible only as long as certain metals have impurities, oxygen and moisture.  The corrosion of metals such as iron is an electrochemical process.  Very pure samples of iron seem to resist corrosion.  In contrast, when a piece of iron containing specks of impurities such as copper are exposed to moist air, the iron becomes pitted with rust spots.  Iron rust is a reddish-brown hydrated compound of varying composition have the formula Fe(OH)3.x H2O.  Therefore, rust spots indicate the location at which pure iron has been oxidized.  These rust spots represent the anode half cells of an electrochemical cell.
Such impurities as copper serve as the cathode half cells and are located in unaffected areas adjacent to the rust spots.   The electrons travel through the iron from the anode to the cathode.  A film of moisture serves as the medium through which ions travel to complete the circuit.
The electrochemical theory of corrosion  is supported by the observation that iron exposed to perfectly dry air does not corrode.  Three half-cell reactions which would present possible reactions in this complex mechanism are:
Anode            Fe(s)  <====>   Fe+2   +   2 e-1
Cathode         1/2 O2(g)  +  H2O(l)  +  2 e-1 <====>   2 OH-1
(neutral sol.)
Cathode          2 H+1  +  2 e-1  <=====>   H2(g)
(acidic sol.)
The overall cell reaction can be obtained by adding the equations.

                        Fe(s)  +  1/2  O2(g) +  H2O(l)  <====>    Fe(OH)2(s)

Further oxidation of Fe(OH)2 yields Fe(OH)3.  Examination of the equation shows that three factors influence the formation of corrosion.   These are the concentration of oxygen, the acidity, and the presence of oxygen.  There is in fact a fourth factor, the quality of the iron metal itself.
To minimize corrosion, protective coatings are applied to prevent the direct contact of moisture and oxygen with the metal.  Electrochemical principles can also be applied to inhibit corrosion.  This is known as cathodic protection.  In cathodic protection, iron is made the cathode half-cell so that it will not lose electrons and dissolve.  This can be accomplished by attaching a more active metal such as magnesium to the iron or by connecting the iron to the cathode of an external power source.  These devices are frequently used to protect underground pipes and tanks and even the hulls of ships.   Steel hulls of ships are especially vulnerable to corrosion because the bronze propeller acts as a cathodic, salt water as a salt bridge and the hull as  an anode.  This problem is resolved by periodically attaching large pieces of magnesium metal to the hull of the ship. Thus the magnesium metal is corroded and dissolved, but the steel hull to which it is attached is protected.