Large protein complexes assemble spontaneously, yet their subunits do not prematurely form unwanted aggregates. This paradox is epitomized in the bacterial flagellar motor, a sophisticated rotary motor and sensory switch consisting of hundreds of subunits. Here we demonstrate that Escherichia coli FliG, one of the earliest-assembling flagellar motor proteins, forms ordered ring structures via domain-swap polymerization, which in other proteins has been associated with uncontrolled and deleterious protein aggregation. Solution structural data, in combination with in vivo biochemical cross-linking experiments and evolutionary covariance analysis, revealed that FliG exists predominantly as a monomer in solution but only as domain-swapped polymers in assembled flagellar motors. We propose a general structural and thermodynamic model for self-assembly, in which a structural template controls assembly and shapes polymer formation into rings.