In both cases, the main motive of the men involved in building these railroads, though not the exclusive but the main motive, was to acquire the subsidies, not to build a railroad. More than that, there was as yet no economic need for a transcontinental railroad. There was not enough freight to justify private investment, but the government, under propaganda similar to today's and such excuses as the prestige of the country, decided to build a railroad and it did so by means of giving subsidies to private groups.
You could very easily substitute "green energy" for "transcontinental railroad" and the quote would be applicable to current government policy. How often do we hear "We are falling behind in green energy technology!" or similar slogans when the government touts its subsidies for such projects?
I guess the old saying is true: "Those who do not learn from history are doomed to repeat it."