Why is the rank of a locally free sheaf well-defined? In Hartshorne p. 109 he defines a sheaf $\mathcal{F}$ of $O_X$-modules to be locally free if there is an open cover of $X$, s.t. on each $U$, $\mathcal{F}|_U$ is a free $O_X|_U$ module of rank $I$. Then if $X$ is connected, rank $I$ is globally well-defined. Here $(X,O_X)$ is any ringed topological space (e.g. not necessarily the structure sheaf of a ring). A similar definition is here: Locally free sheaf - Encyclopedia of Mathematics.
However, it didn't seem obvious to me that if $V$ is a smaller open set included in $U$ (say $U$ connected), then the number of copies $J$ of $(O_X|_V)^J=\mathcal{F}|_V$ would remain the same, because in general the restriction map of the sheaf $O_X$ or $\mathcal{F}$ from $U$ to $V$ need be neither subjective nor injective, why would the index $J$ stay the same as $I$?
 A: Let $\mathcal{F}$ be a locally free sheaf on $X$. For any $x$ in $X$ there exists $x \in U \subset_{open} X $ such that 

$\mathcal{F}|_U \cong \mathcal{O}_X|_U^{(I)}$    $ \ \ \ \ (\star)$.


In particular, for each $y$ in this particular $U$, one has $\mathcal{F}_y \cong \mathcal{O}_{X,y}^{(I)}$ (which is given by the isomorphism above!!!).

Suppose now $X$ is connected and $\mathcal{F}$ is locally free (we need this). Fix an indexing set $I$ (and I think I need to take this $I$ to be one of the indexing sets from $(\star)$ above). The properties of $\mathcal{F}$ show that the set 

$S_I = \left(x \in X : \mathcal{F}_x \cong \mathcal{O}_{X,x}^{(I)}\right)$

is both closed and open in $X$. We know that there exists 

$x$ in $X$ with $\mathcal{F}_x \cong \mathcal{O}_{X,x}^{(I)}$, 

we have $S_I = X$.

In particular, $\text{rank}_{\mathcal{O}_{X,x}}(\mathcal{F}_x)$ is constant as $x$ varies in $X$.

A: The question is answered, but I think a comment has to be made:
When $(X,\mathcal{O}_X)$ is an arbitrary ringed space, the sheaf $\mathcal{O}_X$ could be supported on some subset of $X$. Outside this support, the stalks are zero and don't have the invariant dimension property. The rank is only defined (as a locally constant function on $X$) when the support of $O_X$ is the whole $X$, which is the case when $(X,\mathcal{O}_X)$ is a locally ringed space such as a scheme.
A: Actually, there are two different restriction maps:


*

*The first one (the one you correctly say is neither surjective nor injective in general) is that on sections: for $\mathcal{F}$ a sheaf on a scheme $X$ and two open subsets $V \subseteq U$, there is a map $\mathcal{F}(U) \to \mathcal{F}(V)$.

*On the other hand, the inclusion map $i_{VU}: V \to U$ induces a functor $i_{VU}^{-1}$ from the category of sheaves on $U$ to the category of sheaves on $V$. This is called restriction of sheaves. By functoriality, $W \subseteq V \subseteq U$ yields $i_{WV}^{-1}\circ i_{VU}^{-1} = i_{WU}^{-1}$, so the notation is usually shortened to just $ -|_{W}$.
The second one is the one that you want to look at: the statement is then that $\mathcal{F}|_{U} \cong \mathcal{O}_X^{\oplus I}|_{U}$ implies $\mathcal{F}|_{V} \cong \mathcal{O}_X^{\oplus I}|_{V}$ for $V \subseteq U$.
A: Locally free means not just that $F(U)$ is a free $O_X(U)$-module for an open cover of $U$'s, but that as a sheaf the restriction $F|_U$ is isomorphic to a direct sum of copies of $O_X|_U$.  In particular, this gives an isomorphism of $F(V)$ with a corresponding sum of copies of $O_X(V)$ for every open subset $V\subseteq U$.
A: In commutative algebra, a module $M$ over a noetherian ring $R$ has a rank if $M_P$ is locally free of the same rank at all associated primes $P$ of $R$. (e.g over a domain, the rank is the generic fiber dimension). This notion works well, and can be easy generalized to locally noetherian schemes.
