Solution (source code)

= Solution

A <line in a Riemannian manifold> is a unit-speed <geodesic> $\gamma:\mathbb R\to M$ that minimizes globally:
$$
d_g(\gamma(s),\gamma(t))=|s-t|
$$
for all $s,t\in\mathbb R$. A connected noncompact manifold is <disconnected at infinity> if some compact set $K$ has a complement with at least two unbounded connected components.

Choose points $p_j$ and $q_j$ in two such components with
$$
d_g(p_j,K)\to\infty,
\qquad
d_g(q_j,K)\to\infty.
$$
The <Hopf-Rinow theorem> supplies a length-minimizing geodesic $\gamma_j$ from $p_j$ to $q_j$. Its image must meet $K$, since otherwise it would connect the two different components of $M\setminus K$. Reparametrize so that $\gamma_j(0)=x_j\in K$. After taking a subsequence, compactness gives $x_j\to x\in K$ and the unit tangent vectors $\dot\gamma_j(0)$ converge to some unit $v\in T_xM$.

Both endpoint parameters tend to infinity because their distances from $K$ do. Smooth dependence of geodesics on initial data therefore makes $\gamma_j$ converge on every compact parameter interval to the complete geodesic
$$
\gamma(t)=\exp_x(tv).
$$
Every finite segment of every $\gamma_j$ minimizes length. Passing to the limit gives $d_g(\gamma(s),\gamma(t))=|s-t|$, so $\gamma$ is a line.