In two ways:
1. Certain metals interact with oxygen much more vigorously than iron, including chromium and magnesium. Also aluminium. Those metals will corrode preferentially, either as part of an alloy or as “sacrificial electrodes” (something commonly used on ships).
2. A lot of those metals produce tough, continuous and well bonded oxide layers that work to protect the metal from futther oxidation. The whole issue with rusting is that for pure iron and common steel alloys, rust cracks and peels off, because it increases in volume (think ice vs. water). There are special steel formulations that create a well behaved rust layer that is just as protective as the aluminium or chromium ones, and doesn’t need painting.
Iron combines with oxygen to form rust because of how chemistry works. The way electrons are arranged in iron atoms makes it “happy” to combine with oxygen and form “iron oxide”, the fancy name for rust.
Stainless steel is an alloy that adds some Chromium to the iron. Chromium is “even more happy” to bond with oxygen, but when it does that it creates a completely different substance that’s really just cosmetically not nice and not as physically different as rust is. The substance it makes takes the form of a thin film, and that film makes it harder for more oxygen to get at the metal. So once the film forms it’s less likely any more gets made. In higher-quality stainless steel this film is so thin we can’t really see it without powerful instruments. But cheaper stainless steel can usually appear to rust (because it has less of the more expensive metals that make it truly stainless.)
So put more simply: it DOES rust a little. But it makes a different kind of rust, and that rust forms a kind of protective coating that stops more from forming.
For stainless steel the chromium and nickel added to the iron does not rust. So when stainless steel is exposed to oxygen the iron atoms will react with the oxygen in the air and turn to rust leaving the chromium and nickel behind as a shiney layer on top. This stopps the corrosion as the air does not go thrugh this shiney layer into the iron under it.
There is another type of alloy which does not corrode called “corten”. This works slightly differently. Normally when steel rusts the iron oxide on the surface is very fragile, like dust. And this allows the air to get under it to expose new steel to corrode. The process is therefore allowed to continue untill all the steel is corroded away. Some metals such as aluminium actually oxidizes much faster then iron but the oxide layer it forms is very hard and does not allow air though, so it protects the rest of the metal. Corten does the same with steel. The alloy is specifically designed so that the iron oxide alloy that covers it will still be quite strong and not flake off like normal rust. It is therefore protecting the steel underneeth from further corrosion, not as good as stainless steel or aluminium but still quite usable. If you see a structure made out of what looks like rusty old steel but it still looks in good condition it is likely made out of corten and is intended to look like that.
Latest Answers