Good question, and one I asked a railroad man fifteen years.
Yes, metal expands - and that WAS the reason for the gaps - and the clickety, clickety, clack, clack when riding the rails, and cooling period when firing Krupps Cannons
HOWEVER,
The modern railway is able to save the pounding on the cars and cargos by laying continuous, welded-steel rails. WHY? HOW?
The expansion of the steel has been controled, all lengthwise, contraction and expansion has been modified into WIDTH, which has little inpact upon carrage, or curves, and requires no expansion spacers.
Sincerely,
Gargoyle