Since ancient Egypt, if not long before, hats have served the practical (protection against heat and cold) and the symbolic (religious function, militaryrank, social status).
The Earl of Wessex, who left the Royal Marines, and Princess Margaret's son Viscount Linley, who also does not hold militaryrank, wore black morning coats.