Rusting is oxidation of iron and requires water and oxygen to proceed. Paint restricts or cuts off water and oxygen from the surface of iron thus preventing oxidation or at least retards the process from completion. The most common method of preventing rust from forming on ferrous metals such as iron or steel is to paint all of the exposed surfaces. The problem is that the tiniest spot where the paint may get damaged will allow rust to form. Paint is just a barrier, and there are many types and grades of paint of which some are unsuitable for metal. The most common cause for paint to fail to protect metal from rust is poor preparation of the surface. It is easy to grab a can of rust preventing spray paint and just spray over the dirt, rust and oily substances on the metal. The areas where the surface was dirty, had oily substances on it, or already had rust will not allow the paint to bind to the metal surface correctly.
Hope you understood