diff --git a/ch03-01-variables-and-mutability.html b/ch03-01-variables-and-mutability.html index dfd15644e..373a20f49 100644 --- a/ch03-01-variables-and-mutability.html +++ b/ch03-01-variables-and-mutability.html @@ -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.
+Not being able to change the value of a variable might have reminded you of
+another programming concept that most languages have: constants. 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 mut with
+constants is not allowed: constants aren't only immutable by default, they're
+always immutable. Constants are declared using the const keyword instead of
+the let keyword, and the type of the value must 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.
Here's an example of a constant declaration where the constant's name is
+MAX_POINTS and its value is set to 100,000. Rust constant naming convention
+is to use all upper case with underscores between words:
const MAX_POINTS: u32 = 100_000;
+
+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.
+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.
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 diff --git a/print.html b/print.html index b6af5bf54..58f040956 100644 --- a/print.html +++ b/print.html @@ -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.
+Not being able to change the value of a variable might have reminded you of
+another programming concept that most languages have: constants. 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 mut with
+constants is not allowed: constants aren't only immutable by default, they're
+always immutable. Constants are declared using the const keyword instead of
+the let keyword, and the type of the value must 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.
Here's an example of a constant declaration where the constant's name is
+MAX_POINTS and its value is set to 100,000. Rust constant naming convention
+is to use all upper case with underscores between words:
const MAX_POINTS: u32 = 100_000;
+
+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.
+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.
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