Rusting occurs when iron reacts with oxygen in the presence of water. This is a form of corrosion that results in a red-brown compound called iron oxide (Fe2O3) forming. Metal objects rust quicker in places where there is much water vapour in the air like in the coastal areas. Corrosion weakens the metal.
We can stop rusting by not allowing oxygen and water coming into contact with metal. We can do this by painting the metal, galvanising it, electroplating, oiling the metal and ships have bars of more reactive metal bars attached to them. The bar corrodes instead of the hull of the ship.