mirror of
https://github.com/rust-lang/book.git
synced 2026-11-30 20:44:32 -06:00
rebuild pages at a97e5a8
This commit is contained in:
@@ -147,6 +147,34 @@ an instance in place may be faster than copying and returning newly allocated
|
||||
instances. With smaller data structures, always creating new instances and
|
||||
writing in a more functional programming style may be easier to reason about,
|
||||
so the lower performance penalty might be worth it to gain that clarity.</p>
|
||||
<h3>Differences Between Variables and Constants</h3>
|
||||
<p>Not being able to change the value of a variable might have reminded you of
|
||||
another programming concept that most languages have: <em>constants</em>. Constants
|
||||
are also values bound to a name that are not allowed to change, but there are a
|
||||
few differences between constants and variables. First, using <code>mut</code> with
|
||||
constants is not allowed: constants aren't only immutable by default, they're
|
||||
always immutable. Constants are declared using the <code>const</code> keyword instead of
|
||||
the <code>let</code> keyword, and the type of the value <em>must</em> be annotated. We're about
|
||||
to cover types and type annotations in the next section, “Data Types,” so don't
|
||||
worry about the details right now. Constants can be declared in any scope,
|
||||
including the global scope, which makes them useful for a value that many parts
|
||||
of your code need to know about. The last difference is that constants may only
|
||||
be set to a constant expression, not the result of a function call or any other
|
||||
value that could only be used at runtime.</p>
|
||||
<p>Here's an example of a constant declaration where the constant's name is
|
||||
<code>MAX_POINTS</code> and its value is set to 100,000. Rust constant naming convention
|
||||
is to use all upper case with underscores between words:</p>
|
||||
<pre><code>const MAX_POINTS: u32 = 100_000;
|
||||
</code></pre>
|
||||
<p>Constants are valid for the entire lifetime of a program, within the scope they
|
||||
were declared in. That makes constants useful for values in your application
|
||||
domain that multiple part of the program might need to know about, such as the
|
||||
maximum number of points any player of a game is allowed to earn or the number
|
||||
of seconds in a year.</p>
|
||||
<p>Documenting hardcoded values used throughout your program by naming them as
|
||||
constants is useful to convey the meaning of that value to future maintainers
|
||||
of the code. It also helps to have only one place in your code that you would
|
||||
need to change if the hardcoded value needed to be updated in the future.</p>
|
||||
<h3>Shadowing</h3>
|
||||
<p>As we saw in the guessing game tutorial in Chapter 2, we can declare new
|
||||
variables with the same name as a previous variables, and the new variable
|
||||
|
||||
+28
@@ -1377,6 +1377,34 @@ an instance in place may be faster than copying and returning newly allocated
|
||||
instances. With smaller data structures, always creating new instances and
|
||||
writing in a more functional programming style may be easier to reason about,
|
||||
so the lower performance penalty might be worth it to gain that clarity.</p>
|
||||
<h3>Differences Between Variables and Constants</h3>
|
||||
<p>Not being able to change the value of a variable might have reminded you of
|
||||
another programming concept that most languages have: <em>constants</em>. Constants
|
||||
are also values bound to a name that are not allowed to change, but there are a
|
||||
few differences between constants and variables. First, using <code>mut</code> with
|
||||
constants is not allowed: constants aren't only immutable by default, they're
|
||||
always immutable. Constants are declared using the <code>const</code> keyword instead of
|
||||
the <code>let</code> keyword, and the type of the value <em>must</em> be annotated. We're about
|
||||
to cover types and type annotations in the next section, “Data Types,” so don't
|
||||
worry about the details right now. Constants can be declared in any scope,
|
||||
including the global scope, which makes them useful for a value that many parts
|
||||
of your code need to know about. The last difference is that constants may only
|
||||
be set to a constant expression, not the result of a function call or any other
|
||||
value that could only be used at runtime.</p>
|
||||
<p>Here's an example of a constant declaration where the constant's name is
|
||||
<code>MAX_POINTS</code> and its value is set to 100,000. Rust constant naming convention
|
||||
is to use all upper case with underscores between words:</p>
|
||||
<pre><code>const MAX_POINTS: u32 = 100_000;
|
||||
</code></pre>
|
||||
<p>Constants are valid for the entire lifetime of a program, within the scope they
|
||||
were declared in. That makes constants useful for values in your application
|
||||
domain that multiple part of the program might need to know about, such as the
|
||||
maximum number of points any player of a game is allowed to earn or the number
|
||||
of seconds in a year.</p>
|
||||
<p>Documenting hardcoded values used throughout your program by naming them as
|
||||
constants is useful to convey the meaning of that value to future maintainers
|
||||
of the code. It also helps to have only one place in your code that you would
|
||||
need to change if the hardcoded value needed to be updated in the future.</p>
|
||||
<h3>Shadowing</h3>
|
||||
<p>As we saw in the guessing game tutorial in Chapter 2, we can declare new
|
||||
variables with the same name as a previous variables, and the new variable
|
||||
|
||||
Reference in New Issue
Block a user