  The rank of a scattered $\mathbb{F}_q$-linear set of $\mathrm{PG}(r-1,q^n)$,
$rn$ even, is at most $rn/2$ as it was proved by Blokhuis and Lavrauw.
Existence results and explicit constructions were given for infinitely many
values of $r$, $n$, $q$ ($rn$ even) for scattered $\mathbb{F}_q$-linear sets of
rank $rn/2$. In this paper we prove that the bound $rn/2$ is sharp also in the
remaining open cases.
  Recently Sheekey proved that scattered $\mathbb{F}_q$-linear sets of
$\mathrm{PG}(1,q^n)$ of maximum rank $n$ yield $\mathbb{F}_q$-linear MRD-codes
with dimension $2n$ and minimum distance $n-1$. We generalize this result and
show that scattered $\mathbb{F}_q$-linear sets of $\mathrm{PG}(r-1,q^n)$ of
maximum rank $rn/2$ yield $\mathbb{F}_q$-linear MRD-codes with dimension $rn$
and minimum distance $n-1$.
