Solution (source code)

= Solution

Take the <free abelian group> $G=\mathbb Z^2$, whose <rank of a group> is two, and its <finite-index subgroup> $H=2\mathbb Z\times\mathbb Z$. The subgroup has index two and is again isomorphic to $\mathbb Z^2$, so its rank is two. The <Nielsen–Schreier formula> would instead give $1+2(2-1)=3$. Hence
$$
\boxed{G=\mathbb Z^2,\qquad H=2\mathbb Z\times\mathbb Z}
$$
is a counterexample.