A gun is a mechanical device that fires projectiles at high velocity, using a propellant such as gunpowder or compressed air. The projectile, is fired through a hollow tube known as the barrel.
In military usage the term "gun" refers to artillery that fires projectiles at high velocity, such as anti-aircraft artillery, field guns, tank guns, anti-tank guns and naval guns.
A rifle is a firearm with a stock and a barrel that has a spiral groove or grooves ("rifling") cut into its interior. The rifling produces areas that make contact with the projectile (a bullet), putting spin on the bullet. When the projectile leaves the barrel, the spin improves its accuracy and range, in the same way that a rugby is spun whilst being thrown for more distance and accuracy. The word "rifle" originally referred to the grooving, and a rifle was called a "rifled gun."
In summary Gun is a general term that is used to refer to most firearms including rifles but can be used as a military term for artillery guns such as those on tanks, anti air and naval.
The name rifle is a term that refers to firearms that have a spiral groove (rifling) to improve the bullets accuracy.
Hope this helps