# How does the divergence theorem justify the integral form of the continuity equation?

I vaguely understand the continuity equation (at least its integral form), but I don't really understand the differential form of the continuity equation. I'm having trouble understanding how to switch back and forth between the forms using the divergence theorem.

The vector field $\mathbf F$ is what we use to describe the flow of a fluid. The divergence of this field describes the expansion or compression of a gas. What the divergence theorem says, is that the total expansion (or compression) of the gas in some volume $V$ is equal to the flux of the fluid out (or in) of the boundary (i.e., how much stuff is leaving (or entering) the surface $S$). Mathematically, this is $$\int_V\nabla\cdot\mathbf F\,dV=\int_{\partial V}\mathbf F\cdot d\mathbf S$$ where $\mathbf F\cdot d\mathbf S$ represents the amount normal to the surface.
So for a volume of mass $m$, the time-rate-of-change of the mass is equal to the above (assuming that there aren't any other sources of matter): $$\frac{\partial m}{\partial t}=-\int_V\nabla\cdot\mathbf F\,dV=-\int_{\partial V}\mathbf F\cdot d\mathbf S$$ which is our integral formulation of the continuity equation.
Since we know that $m=\int\rho\,dV$, then the above is $$\frac{\partial}{\partial t}\int\rho\,dV=-\int_V\nabla\cdot\mathbf F\,dV=-\int_{\partial V}\mathbf F\cdot d\mathbf S$$ And since temporal and spatial coordinates are orthogonal, we can swap them to get $$\int\frac{\partial\rho}{\partial t}\,dV=-\int_V\nabla\cdot\mathbf F\,dV=-\int_{\partial V}\mathbf F\cdot d\mathbf S$$ And lastly, since the volume is arbitrary, then the left two terms above must be $$\frac{\partial\rho}{\partial t}=-\nabla\cdot\mathbf F$$ which is our differential form of the continuity equation.