For a metric space $X$, the following are equivalent:
- Every continuous map from $X$ to another metric space is uniformly continuous.
- $X$ is complete and almost totally bounded.
Definition. $X$ is almost totally bounded if for every $r>0$ there is a finite set
$\{x_1,\dots,x_n\}\in X$ and a number $\delta>0$ such that for any distinct points
$a,b\notin \bigcup_{i=1}^n B(x_i,r)$ we have $d(a,b)\ge \delta$. (In other words,
$X\setminus \bigcup_{i=1}^n B(x_i,r)$ is uniformly separated.) Here $B(x,r)=\{y:d(x,y)\le r\}$.
For example, the union of $\mathbb Z$ and of any compact subset of $\mathbb R$ satisfies 2.
Another example is the set $\{0\}\cup \{k^{-1}e_n:k,n=1,2,\dots\}\in \ell^2$ where $\{e_n\}$ is an orthonormal basis of $\ell^2$. This example is interesting because the set cannot be written as a union of a compact
and uniformly separated sets.
Proof. Suppose 2 holds but 1 fails. Let $f:X\to Y$ be a continuous map that is not uniformly continuous.
Then there is $\epsilon>0$ and two sequences $p_n,q_n$ in $X$ such that $d_X(p_n,q_n)\to 0$ but $d_Y(p_n,q_n)\ge \epsilon$ for all $n$. Let $B(x_i,r)$ be as in the definition of almost totally bounded. Since $d_X(p_n,q_n)\to 0$, for all sufficiently large $n$ we have either $p_n$ or $q_n$ contained in $\bigcup_{i=1}^n B(x_i,r)$. Therefore, there is a ball $B(x_j,r)$ that contains, say, $p_n$ for infinitely many values of $n$. Consequently, $B(x_j,2r)$ contains both $p_n$ and $q_n$ for infinitely many values of $n$.
Since $B(x_j,2r)$ is also complete and almost totally bounded, we can repeat the above with a smaller value of
$r$, and with $X$ replaced by $B(x_j,2r)$. The result is a nested sequence of shrinking closed balls $B_k$, each of which
contains $p_n$ and $q_n$ for infinitely many values of $n$. By completeness, $\bigcap B_k$ contains a point
$x$. The continuity of $f$ at $x$ contradicts the assumption that $d_Y(p_n,q_n)\ge \epsilon$ for all $n$.
Now suppose 2 fails. This can happen in two ways. (a) $X$ is not complete. Consider it as a subset of its completion $\overline{X}$. Pick
$a\in \overline{X}\setminus X$. The function $f(x)=1/d_{\overline X}(x,a)$ is continuous on $X$, but not
uniformly continuous. Indeed, there is a Cauchy sequence $x_n$ in $X$ such that $f(x_n)\to\infty$.
(b) $X$ is not almost totally bounded. Then there exists $r>0$ for which $X$ contains an infinite sequence of
points $p_n$ such that $d(p_n,p_m)>r$ whenever $n\ne m$, and additionally
$\operatorname{dist}(p_n,X\setminus \{p_n\})\to 0$. The latter condition allows us to choose $q_n\ne p_n$
so that $d(p_n,q_n)\to 0$.
Define $$f_n(x) = \max\left(0, \;1- \frac{d(x,p_n)}{d(p_n,q_n)} \right),\quad n=1,2,\dots$$
and observe that $f_n(p_n)=1$, $f_n(q_n)=0$, and for large enough $n$ the supports of $f_n$ are disjoint.
Thus, the function $\sum_n f_n$ is continuous on $X$, but by its construction it is not uniformly continuous.