I remember doing this in Super Mario 64 DS. You can keep doing it without picking up the hat, making at least 40 hats...
Cool. I never knew that.