Traversal of a symmetry-breaking phase transition at finite rates can lead to causally separated regions with incompatible symmetries and the formation of defects at their boundaries, which has a crucial role in quantum and statistical mechanics, cosmology and condensed matter physics. This mechanism is conjectured to follow universal scaling laws prescribed by the Kibble-Zurek mechanism. Here we determine the scaling law for defect formation in a crystal of 16 laser-cooled trapped ions, which are conducive to the precise control of structural phases and the detection of defects. The experiment reveals an exponential scaling of defect formation γ(β), where γ is the rate of traversal of the critical point and β=2.68±0.06. This supports the prediction of β=8/3≈2.67 for finite inhomogeneous systems. Our result demonstrates that the scaling laws also apply in the mesoscopic regime and emphasizes the potential for further tests of non-equilibrium thermodynamics with ion crystals.