Consider a rank-factorization $p = cf$ where $f:\Bbb R^n \to \Bbb R^r$ and $c:\Bbb R^r \to \Bbb R^n$, where $r = \operatorname{rank}(p)$, $c$ is injective, and $f$ is surjective. We note that $$ p^2 = p \implies cfcf = cf. $$ That is, we have $c(fc)f = c(\operatorname{id}_{\Bbb R^r})f$. Because $f$ has a right-inverse and $c$ has a left-inverse, we may conclude from the above that $fc = \operatorname{id}_{\Bbb R^r}$. It follows that $$ \operatorname{trace}(p) = \operatorname{trace}(cf) = \operatorname{trace}(fc) = \operatorname{trace}(\operatorname{id}_{\Bbb R^r}) = r $$ which was the desired result.