Formation of rust is an electrochemical phenomenon. Formula of rust is
Fe3O4 .xH2O
Fe2O3.xH2O
FeO.xH2O
Fe3O4
Fe→Fe+2 + 2e- . Fe+2 formed at anodic site on the surface of Iron oxidizes to Fe+3 in presence atmospheric oxygen to form Fe2O3 .xH2O (a red layer)