Second, in most modern rail installations the track is pre-tensioned to deal with thermal expansion. The track is stretched with a hydraulic tool and welded while under tension, so thermal expansion just means reduced tension. This is why trains on modern rail lines don't make the familiar click-clack sound.
(How much tension is applied depends on the country and weather - you can choose between tolerating high temperatures and tolerating low temperatures. This is why in the UK, there are sometimes train stoppages in very hot weather)