Dear Kwan, since your profile says you are interested in Algebraic Geometry, here are geometric considerations that might appeal to you.

Consider a projective module $P$ of finite type over a commutative ring $A$. It corresponds to a locally free sheaf $\mathcal F $ over $X=spec(A)$. The rank of $\mathcal F $ at the prime ideal $\mathfrak p$ is that of the *free* $A_{\mathfrak p}$-module $\mathcal F_{\mathfrak p}$.
The rank is then a locally constant function on $X$ and if $X$ is connected (this means that the nilpotent radical of $A$ is a prime ideal) it may be seen as an integer. 

If  $A$ is a domain, then $X$ is certainly connected and has a generic point $\eta$ whose local ring is the field of fractions $\mathcal O_\eta=K=Frac(A)$. The rank of $\mathcal F $  or of $P$ is then simply the dimension of the $K$ vector space $P\otimes_A K$. 

Actually, if $A$ is a domain, this formula can be used to define the rank of any $A$-module $M$ (projective or not, finitely generated or not) : $rank(M)=dim_K ( P\otimes_A K) $ . This is the definition given by Matsumura in his book *Commutative Rings*, page 84. It corresponds to the **maximum** number of elements of $M$ which are linearly independent over $A$. 

The **minimum** number of generators of $M$  ( which started this discussion) is quite a different, but interesting invariant, which has been studied by Forster, Swan, Eisenbud, Evans,... Geometrically it corresponds to the minimum numbers of global sections of $\tilde{M}$ which generate this sheaf at each point of $spec(A)$.  Elementary fairly well known example:   every ideal of a Dedekind domain can be generated by two elements.