Minecraft wiki entry for Piston, associated text: Rails: as long as they remain on top of a solid face of a block in their new position, and that block isn't moving at the same time.
An exception is when the rail and block supporting it are on two parallel extended pistons at which the rail remains attached. Trying to move both on the same piston using slime blocks does not work, nor does moving them on perpendicular pistons (although the latter temporarily appears to work because of a bug MC-75716).
Rails re-orient themselves after being pushed, similar to when placed manually.
Carpets