A lot depends on the skill level and mindset of the victim. Most civilian gun owners are relatively untrained, meaning they don't take regular instruction and frequent live-fire practice. Your average victim is thinking about their grocery list and how much they don't like that one guy at work, they're not using a military bearing and being aware of their surroundings. If you can spot the assailant at the end of those 20ft and you have a rifle, deploy the rifle, but most people won't even notice the assault until the perp is right on top of them, and at that point bringing a long gun out will just get it taken away from you, or at least grappled to a position where it can't shoot the perp. A handgun, however, is much easier to retain against someone already on you, and easier to place effective shots to end the threat. It's a matter of degree, of the skill of the victim. Generally speaking, handguns are better than long guns for CQB, for the typical relatively untrained civilian victim.
A handgun. You used a handgun, not a rifle.