Ownership
-Rust’s central feature is called ‘ownership’. It is a feature that is +
Rust’s central feature is called ownership. It is a feature that is straightforward to explain, but has deep implications for the rest of the language.
All programs have to manage the way they use a computer's memory while running. @@ -103,8 +103,8 @@ at which it’s declared until the end of the current scope. That is:
In other words, there are two important points in time here:
-
-
- When
scomes ‘into scope’, it is valid.
- - It remains so until it ‘goes out of scope’. +
- When
scomes "into scope", it is valid.
+ - It remains so until it "goes out of scope".
At this point, things are similar to other programming languages. Now let’s
build on top of this understanding by introducing the String type.
String.
That first part is done by us: when we call String::from(), its
implementation requests the memory it needs. This is pretty much universal in
programming languages.
The second case, however, is different. In languages with a garbage collector -(‘GC’), the GC will keep track and clean up memory that isn't being used +
The second case, however, is different. In languages with a garbage collector +(GC), the GC will keep track and clean up memory that isn't being used anymore, and we, as the programmer, don’t need to think about it. Without GC, it’s our responsibility to identify when memory is no longer being used and call code to explicitly return it, just as we did to request it. Doing this @@ -257,13 +257,13 @@ alone will free the memory, and we’re done.
This leads us to the Ownership Rules:
-
- Each value in Rust has a variable binding that’s called its ‘owner’.
+- Each value in Rust has a variable binding that’s called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be
drop()ped.
Furthermore, there’s a design choice that’s implied by this: Rust will never -automatically create ‘deep’ copies of your data. Therefore, any automatic +automatically create "deep" copies of your data. Therefore, any automatic copying can be assumed to be inexpensive.
Clone
But what if we do want to deeply copy the String’s data and not just the
diff --git a/ch04-02-references-and-borrowing.html b/ch04-02-references-and-borrowing.html
index 47e6f5e01..549393f52 100644
--- a/ch04-02-references-and-borrowing.html
+++ b/ch04-02-references-and-borrowing.html
@@ -106,7 +106,7 @@ fn calculate_length(s: &String) -> usize {
function return value is gone. Next, note that we pass &s1 into
calculate_length(), and in its definition, we take &String rather than
String.
These &s are called ‘references’, and they allow you to refer to some value
+
These &s are called references, and they allow you to refer to some value
without taking ownership of it. Here’s a diagram:
DIAGRAM GOES HERE of a &String pointing at a String, with (ptr, len, capacity)
Let’s take a closer look at the function call here:
@@ -135,7 +135,7 @@ a reference as an argument. Let’s add some explanatory annotations: we don’t drop what a reference points to when the reference goes out of scope. This lets us write functions which take references as arguments instead of the values themselves, so that we won’t need to return them to give back ownership. -There’s another word for what references do, and that’s ‘borrowing’. Just like +
There’s another word for what references do, and that’s borrowing. Just like with real life, if a person owns something, you can borrow it from them. When you’re done, you have to give it back.
Speaking of which, what if you try to modify something you borrow from me? Try @@ -259,7 +259,7 @@ fn dangle() -> &String { error: aborting due to previous error
This error message refers to a feature we haven’t learned about yet, -‘lifetimes’. The message does contain the key to why this code is a problem, +lifetimes. The message does contain the key to why this code is a problem, though:
this function’s return type contains a borrowed value, but there is no value for it to be borrowed from diff --git a/ch05-00-structs.html b/ch05-00-structs.html index 30cd1313b..d474db03e 100644 --- a/ch05-00-structs.html +++ b/ch05-00-structs.html @@ -132,7 +132,7 @@ modules and namespaces in depth yet, but you can think of the, the same as the type. Thepowi()f64powi()function takes two arguments: the first is a number, and the second is the power that it raises that number to. In this -case, the second number is an integer, hence the ‘i’ in its name. Similarly, +case, the second number is an integer, hence theiin its name. Similarly,sqrt()is a function under thef64module, which takes the square root of its argument.Why
diff --git a/ch05-01-method-syntax.html b/ch05-01-method-syntax.html index 4d6a649f8..cf62ce952 100644 --- a/ch05-01-method-syntax.html +++ b/ch05-01-method-syntax.html @@ -68,8 +68,8 @@structs?Method Syntax
-In the last section on ownership, we made several references to ‘methods’. -Methods look like this:
+In Chapter 4 when we discussed ownership, we made several references to +methods. Methods look like this:
-let s1 = "hello"; // call a method on s1 @@ -77,8 +77,8 @@ let s2 = s1.clone(); println!("{}", s1);The call to
+clone()is attached tos1with a dot. This is called ‘method -syntax’, and it’s a way to call certain functions with a different style.The call to
clone()is attached tos1with a dot. This is called method +syntax, and it’s a way to call certain functions with a different style.Why have two ways to call functions? We’ll talk about some deeper reasons related to ownership in a moment, but one big reason is that methods look nicer when chained together:
@@ -95,7 +95,7 @@ x.f().g().h(); methods.Defining methods
We can define methods with the
+implementation. Doing so looks like this:implkeyword.implis short for -‘implementation’. Doing so looks like this:#[derive(Debug,Copy,Clone)] struct Point { x: f64, @@ -166,7 +166,7 @@ from using the value ofselfafter the transformation happens.When we defined
-distance(), we took bothselfand the other argument by reference. Yet, we needed a&forp2but notp1. What gives?This feature is called ‘automatic referencing’, and calling methods is one +
This feature is called automatic referencing, and calling methods is one of the few places in Rust that has behavior like this. Here’s how it works: when you call a method with
diff --git a/ch06-01-option.html b/ch06-01-option.html index 5356a78a3..2a775fe91 100644 --- a/ch06-01-option.html +++ b/ch06-01-option.html @@ -72,7 +72,7 @@ feature that we talked a little bit about in the previous chapter: generics.self.(, Rust will automatically add in&s or&muts to match the signature. In other words, these are the same:Programming language design is often thought of as which features you include, but it's also about which features you leave out. Rust does not have a feature -that is in many other languages: 'null'. In languages with this feature, +that is in many other languages: null. In languages with this feature, variables can have two states: null or not-null.
The inventor of this concept has this to say:
@@ -117,7 +117,7 @@ let absent_number: Option<i32> = None;Let's dig in. First, you'll notice that we used the
<T>syntax when definingOption<T>: it's a generic enum.Option<T>has two variants:Some, which contains aT, andNone, which has no data associated with it. In some -sense,Nonemeans 'null', andSomemeans 'not null'. So why is this any +sense,Nonemeans "null", andSomemeans "not null". So why is this any better than null?In short, because
diff --git a/chXX-patterns.html b/chXX-patterns.html index 750e30091..405be600f 100644 --- a/chXX-patterns.html +++ b/chXX-patterns.html @@ -111,12 +111,12 @@ toOption<T>andTare different types. That's a bit too short though. Here's an example:y. We could have used twoletstatements as well: multiple bindings at once is nice. As we become more proficient in Rust, we’ll figure out which style is better, but it’s mostly a judgement call.Type annotations
-Most of the time, Rust uses ‘type inference’, meaning that it attempts to infer +
Most of the time, Rust uses type inference, meaning that it attempts to infer the types of your bindings rather than you having to declare them explicitly even though Rust is a statically typed language. Occasionally, Rust won't have enough information to infer the type of your value, and you will need to add a type annotation in with the pattern.
-Here’s what a
+letstatement with a ‘type annotation’ looks like:Here’s what a
letstatement with a type annotation looks like:-fn main() { let x: i32 = 5; } diff --git a/chZZ-generics.html b/chZZ-generics.html index 5b242248c..fc1cc4593 100644 --- a/chZZ-generics.html +++ b/chZZ-generics.html @@ -101,8 +101,8 @@ struct Point<T> {Our previous definition said, "We are defining a struct named Point." This definition says something slightly different: "We are defining a struct named Point with one type parameter
-T."Let's talk about this term 'type parameter'. We've already seen one other thing -called a 'parameter' in Rust: function parameters:
+Let's talk about this term type parameter. We've already seen one other thing +called a "parameter" in Rust: function parameters:
fn plus_one(x: i32) -> i32 { x + 1 } @@ -149,7 +149,7 @@ let same = Point { x: 5.0, y: 5.0 };Here, instead of a single parameter,
diff --git a/print.html b/print.html index 7899f344e..9f9654fa1 100644 --- a/print.html +++ b/print.html @@ -265,7 +265,7 @@ programs. manage their Rust projects because it makes a lot of tasks easier. For example, Cargo takes care of building your code, downloading the libraries your code depends on, and building those libraries. We call libraries your code needs -‘dependencies’ since your code depends on them. +dependencies since your code depends on them.T, we have two:TandOtherT. Type parameters have the same naming convention as other types:CamelCase. However, you'll often see short, one-letter names used for types.Tis very -common, because it's short for 'type', but you can name them something longer +common, because it's short for "type", but you can name them something longer if you'd like. In this version ofPoint, we say thatxhas the typeT, andyhas the typeOtherT. This lets us give them two different types, but they don't have to be.The simplest Rust programs, like the one we've written so far, don’t have any dependencies, so right now, you'd only be using the part of Cargo that can take care of building your code. As you write more complex Rust programs, you’ll @@ -440,7 +440,7 @@ authors = ["Your Name <you@example.com>"]
If the authors information that Cargo got from your environment is not correct, go ahead and fix that.
-And as we saw in the last chapter,
cargo newgenerates a ‘Hello, world!’ for +And as we saw in the last chapter,
cargo newgenerates a "Hello, world!" for us. Check outsrc/main.rs:fn main() { println!("Hello, world!"); @@ -480,7 +480,7 @@ fn main() {We’ll need to take user input and then print the result as output. As such, we need the
iolibrary from the standard library. Rust only imports a few things -by default into every program, the ‘prelude’. If it’s not in the +by default into every program, the prelude. If it’s not in the prelude, you’ll have touseit directly. Using thestd::iolibrary gets you a number of usefulio-related things, so that's what we've done here.fn main() { @@ -499,11 +499,11 @@ prints a string to the screen.Now we’re getting interesting! There’s a lot going on in this little line. The first thing to notice is that this is a let statement, which is -used to create what are called ‘variable bindings’. Here's an example:
+used to create what are called variable bindings. Here's an example:let foo = bar;This will create a new binding named
foo, and bind it to the valuebar. In -many languages, this is called a ‘variable’, but Rust’s variable bindings have +many languages, this is called a variable, but Rust’s variable bindings have a few tricks up their sleeves.For example, they’re immutable by default. That’s why our example uses
@@ -517,10 +517,10 @@ everything in comments. bound to:mut: it makes a binding mutable, rather than immutable.String::new().-
Stringis a string type, provided by the standard library. AStringis a growable, UTF-8 encoded bit of text.The
::new()syntax uses::because this is an ‘associated function’ of +The
+static method.::new()syntax uses::because this is an associated function of a particular type. That is to say, it’s associated withStringitself, rather than a particular instance of aString. Some languages call this a -‘static method’.This function is named
@@ -544,7 +544,7 @@ terminal. More specifically, a std::io::St passing one argument tonew(), because it creates a new, emptyString. You’ll find anew()function on many types, as it’s a common name for making a new value of some kind.read_line():&mut guess.Remember how we bound
guessabove? We said it was mutable. However,read_linedoesn’t take aStringas an argument: it takes a&mut String. -The&is the feature of Rust called a ‘reference’, which allows you to have +The&is the feature of Rust called a reference, which allows you to have multiple ways to access one piece of data in order to reduce copying. References are a complex feature, as one of Rust’s major selling points is how safe and easy it is to use references. We don’t need to know a lot of those @@ -623,9 +623,9 @@ print it back out.Generating a secret number
Next, we need to generate a secret number. Rust does not yet include random number functionality in its standard library. The Rust team does, however, -provide a
randcrate. A ‘crate’ is a package of Rust code. -We’ve been building a ‘binary crate’, which is an executable.randis a -‘library crate’, which contains code that’s intended to be used with other +provide arandcrate. A crate is a package of Rust code. +We’ve been building a binary crate, which is an executable.randis a +library crate, which contains code that’s intended to be used with other programs.Using external crates is where Cargo really shines. Before we can write the code using
rand, we need to modify ourCargo.toml. Open it up, and @@ -688,9 +688,9 @@ again. This lets you have a repeatable build automatically. In other words, we’ll stay at0.3.14until we explicitly upgrade, and so will anyone who we share our code with, thanks to the lock file.What about when we do want to use
v0.3.15? Cargo has another command, -update, which says ‘ignore theCargo.lockfile and figure out all the +update, which says "ignore theCargo.lockfile and figure out all the latest versions that fit what we’ve specified inCargo.toml. If that works, -write those versions out to the lock file’. But by default, Cargo will only +write those versions out to the lock file". But by default, Cargo will only look for versions larger than0.3.0and smaller than0.4.0. If we want to move to0.4.x, we’d have to update what is in theCargo.tomlfile. When we do, the next time wecargo build, Cargo will update the index and re-evaluate @@ -729,7 +729,7 @@ dependency. This also does the equivalent of ause rand;as well, s call anything in therandcrate by prefixing it withrand::.Next, we added another
useline:use rand::Rng. We’re going to use a method in a moment, and it requires thatRngbe in scope to work. The basic -idea is this: methods are defined on something called ‘traits’, and for the +idea is this: methods are defined on something called traits, and for the method to work, it needs the trait to be in scope. For more about the details, read the traits section in Chapter XX.There are two other lines we added, in the middle:
@@ -814,7 +814,7 @@ use that type: a reference to the thing you want to compare it to. It returns theOrderingtype we imported with theusestatement earlier. We use amatchstatement to determine exactly what kind ofOrderingit is.Orderingis an -enum, short for ‘enumeration’, which looks like this: +enum, short for enumeration, which looks like this:enum Foo { Bar, Baz, @@ -825,7 +825,7 @@ statement to determine exactly what kind ofOrderingit is.O namespace for a particularenumvariant.The
Orderingenumhas three possible variants:Less,Equal, andGreater. Thematchstatement takes a value of a type and lets you -create an ‘arm’ for each possible value. An arm is made up of a pattern and the +create an arm for each possible value. An arm is made up of a pattern and the code that we should execute if the pattern matches the value of the type. Since we have three types ofOrdering, we have three arms:-match guess.cmp(&secret_number) { @@ -848,7 +848,7 @@ src/main.rs:23:21: 23:35 note: found type `&_` error: aborting due to previous error Could not compile `guessing_game`.Whew! This is a big error. The core of it is that we have ‘mismatched types’. +
Whew! This is a big error. The core of it is that we have mismatched types. Rust has a strong, static type system. However, it also has type inference. When we wrote
let guess = String::new(), Rust was able to infer thatguessshould be aString, so it doesn’t make us write out the type. With our @@ -898,7 +898,7 @@ fn main() { .expect("Please type a number!");Wait a minute, didn't we already have a
guess? We do, but Rust allows us -to ‘shadow’ the previousguesswith a new one. This is often used in this +to shadow the previousguesswith a new one. This is often used in this exact situation, whereguessstarts as aString, but we want to convert it to au32. Shadowing lets us re-use theguessname rather than forcing us to come up with two unique names likeguess_strandguessor something @@ -909,8 +909,8 @@ else.Here,
guessrefers to the oldguess, the one that was aStringwith our input in it. Thetrim()method onStrings will eliminate any white space at the beginning and end of our string. This is important, as we had to press the -‘return’ key to satisfyread_line(). If we type5and hit return,guess-looks like this:5\n. The\nrepresents ‘newline’, the enter key.trim()+"return" key to satisfyread_line(). If we type5and hit return,guess+looks like this:5\n. The\nrepresents "newline", the return key.trim()gets rid of this, leaving our string with only the5.The
parse()method on strings parses a string into some kind of number. Since it can parse a variety of numbers, we need to give Rust a hint as @@ -1090,8 +1090,8 @@ fn main() { Err(_) => continue, };This is how you generally move from ‘crash on error’ to ‘actually handle the -error’: by switching from
expect()to amatchstatement. AResultis the +This is how you generally move from "crash on error" to "actually handle the +error": by switching from
expect()to amatchstatement. AResultis the return type ofparse().Resultis anenumlikeOrdering, but in this case, each variant has some data associated with it.Okis a success, andErris a failure. Each contains more information: in this case, the @@ -1306,7 +1306,7 @@ depends on the tradeoffs you want to make in your situation.Shadowing
As we saw in the guessing game tutorial, we can declare new bindings with the same name as a previous binding, and the new binding shadows the previous -binding. We say that the first binding is ‘shadowed’ by the second, which means +binding. We say that the first binding is shadowed by the second, which means that the second binding's value is what you will see when you use the variable after the second binding. This can be useful if you’d like to perform a few transformations on a value, but have the binding be immutable after those @@ -1500,7 +1500,7 @@ this tuple value, like this:
In this program, we first create a tuple and bind it to the name
tup. We then use a pattern withletto taketupand turn it into three separate -bindings,x,y, andz. This is called ‘destructuring’, because it breaks +bindings,x,y, andz. This is called destructuring, because it breaks the single tuple into three parts.Finally, we print the value of
y, which is6.4.Tuple Indexing
@@ -1891,7 +1891,7 @@ $ cd branches case, our condition is checking if our variable bindingnumberhas a value that is less than 5. The block of code we want to execute if the condition is true goes immediately after the condition, inside curly braces. These blocks -are sometimes called ‘arms’. We can optionally also include anelse+are sometimes called arms. We can optionally also include anelsestatement, which we have chosen to do here.elsegives the program a block of code to execute shouldconditionevaluate to false.Try running this code, and you should see output like this:
@@ -2022,7 +2022,7 @@ single type. If we try to run this, we’ll get an error:Compiling branches v0.1.0 (file:///projects/branches) src/main.rs:4:18: 8:6 error: if and else have incompatible types: expected `_`, - found `&‘static str` + found `&'static str` (expected integral variable, found &-ptr) [E0308] src/main.rs:4 let number = if condition { @@ -2180,7 +2180,7 @@ enables Rust to make memory safety guarantees without needing a garbage collector. We’ll also talk about several related features: borrowing, slices, and how Rust lays things out in memory.Ownership
-Rust’s central feature is called ‘ownership’. It is a feature that is +
Rust’s central feature is called ownership. It is a feature that is straightforward to explain, but has deep implications for the rest of the language.
All programs have to manage the way they use a computer's memory while running. @@ -2215,8 +2215,8 @@ at which it’s declared until the end of the current scope. That is:
In other words, there are two important points in time here:
-
- When
-scomes ‘into scope’, it is valid.- It remains so until it ‘goes out of scope’.
+- When
+scomes "into scope", it is valid.- It remains so until it "goes out of scope".
At this point, things are similar to other programming languages. Now let’s build on top of this understanding by introducing the
@@ -2259,8 +2259,8 @@ done with ourStringtype.String.That first part is done by us: when we call
-String::from(), its implementation requests the memory it needs. This is pretty much universal in programming languages.The second case, however, is different. In languages with a garbage collector -(‘GC’), the GC will keep track and clean up memory that isn't being used +
The second case, however, is different. In languages with a garbage collector +(GC), the GC will keep track and clean up memory that isn't being used anymore, and we, as the programmer, don’t need to think about it. Without GC, it’s our responsibility to identify when memory is no longer being used and call code to explicitly return it, just as we did to request it. Doing this @@ -2369,13 +2369,13 @@ alone will free the memory, and we’re done.
This leads us to the Ownership Rules:
-
- Each value in Rust has a variable binding that’s called its ‘owner’.
+- Each value in Rust has a variable binding that’s called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be
drop()ped.Furthermore, there’s a design choice that’s implied by this: Rust will never -automatically create ‘deep’ copies of your data. Therefore, any automatic +automatically create "deep" copies of your data. Therefore, any automatic copying can be assumed to be inexpensive.
Clone
But what if we do want to deeply copy the
-String’s data and not just the @@ -2589,7 +2589,7 @@ fn calculate_length(s: &String) -> usize { function return value is gone. Next, note that we pass&s1intocalculate_length(), and in its definition, we take&Stringrather thanString.These
&s are called ‘references’, and they allow you to refer to some value +These
&s are called references, and they allow you to refer to some value without taking ownership of it. Here’s a diagram:DIAGRAM GOES HERE of a &String pointing at a String, with (ptr, len, capacity)
Let’s take a closer look at the function call here:
@@ -2618,7 +2618,7 @@ a reference as an argument. Let’s add some explanatory annotations: we don’t drop what a reference points to when the reference goes out of scope. This lets us write functions which take references as arguments instead of the values themselves, so that we won’t need to return them to give back ownership. -There’s another word for what references do, and that’s ‘borrowing’. Just like +
There’s another word for what references do, and that’s borrowing. Just like with real life, if a person owns something, you can borrow it from them. When you’re done, you have to give it back.
Speaking of which, what if you try to modify something you borrow from me? Try @@ -2742,7 +2742,7 @@ fn dangle() -> &String { error: aborting due to previous error
This error message refers to a feature we haven’t learned about yet, -‘lifetimes’. The message does contain the key to why this code is a problem, +lifetimes. The message does contain the key to why this code is a problem, though:
this function’s return type contains a borrowed value, but there is no value for it to be borrowed from @@ -3090,7 +3090,7 @@ modules and namespaces in depth yet, but you can think of the, the same as the type. Thepowi()f64powi()function takes two arguments: the first is a number, and the second is the power that it raises that number to. In this -case, the second number is an integer, hence the ‘i’ in its name. Similarly, +case, the second number is an integer, hence theiin its name. Similarly,sqrt()is a function under thef64module, which takes the square root of its argument.Why
@@ -3265,8 +3265,8 @@ Point 1: Point { x: 0, y: 5 }structs?You’ll see this repeated later with other types. We’ll cover traits fully in Chapter XX.
Method Syntax
-In the last section on ownership, we made several references to ‘methods’. -Methods look like this:
+In Chapter 4 when we discussed ownership, we made several references to +methods. Methods look like this:
-let s1 = "hello"; // call a method on s1 @@ -3274,8 +3274,8 @@ let s2 = s1.clone(); println!("{}", s1);The call to
+clone()is attached tos1with a dot. This is called ‘method -syntax’, and it’s a way to call certain functions with a different style.The call to
clone()is attached tos1with a dot. This is called method +syntax, and it’s a way to call certain functions with a different style.Why have two ways to call functions? We’ll talk about some deeper reasons related to ownership in a moment, but one big reason is that methods look nicer when chained together:
@@ -3292,7 +3292,7 @@ x.f().g().h(); methods.Defining methods
We can define methods with the
+implementation. Doing so looks like this:implkeyword.implis short for -‘implementation’. Doing so looks like this:#[derive(Debug,Copy,Clone)] struct Point { x: f64, @@ -3363,7 +3363,7 @@ from using the value ofselfafter the transformation happens.When we defined
-distance(), we took bothselfand the other argument by reference. Yet, we needed a&forp2but notp1. What gives?This feature is called ‘automatic referencing’, and calling methods is one +
This feature is called automatic referencing, and calling methods is one of the few places in Rust that has behavior like this. Here’s how it works: when you call a method with
@@ -3535,7 +3535,7 @@ useful:self.(, Rust will automatically add in&s or&muts to match the signature. In other words, these are the same:Option. feature that we talked a little bit about in the previous chapter: generics.Programming language design is often thought of as which features you include, but it's also about which features you leave out. Rust does not have a feature -that is in many other languages: 'null'. In languages with this feature, +that is in many other languages: null. In languages with this feature, variables can have two states: null or not-null.
The inventor of this concept has this to say:
@@ -3580,7 +3580,7 @@ let absent_number: Option<i32> = None;Let's dig in. First, you'll notice that we used the
<T>syntax when definingOption<T>: it's a generic enum.Option<T>has two variants:Some, which contains aT, andNone, which has no data associated with it. In some -sense,Nonemeans 'null', andSomemeans 'not null'. So why is this any +sense,Nonemeans "null", andSomemeans "not null". So why is this any better than null?In short, because
@@ -3916,8 +3916,8 @@ struct Point<T> {Option<T>andTare different types. That's a bit too short though. Here's an example:Our previous definition said, "We are defining a struct named Point." This definition says something slightly different: "We are defining a struct named Point with one type parameter
-T."Let's talk about this term 'type parameter'. We've already seen one other thing -called a 'parameter' in Rust: function parameters:
+Let's talk about this term type parameter. We've already seen one other thing +called a "parameter" in Rust: function parameters:
fn plus_one(x: i32) -> i32 { x + 1 } @@ -3964,7 +3964,7 @@ let same = Point { x: 5.0, y: 5.0 };Here, instead of a single parameter,
@@ -4069,12 +4069,12 @@ toT, we have two:TandOtherT. Type parameters have the same naming convention as other types:CamelCase. However, you'll often see short, one-letter names used for types.Tis very -common, because it's short for 'type', but you can name them something longer +common, because it's short for "type", but you can name them something longer if you'd like. In this version ofPoint, we say thatxhas the typeT, andyhas the typeOtherT. This lets us give them two different types, but they don't have to be.y. We could have used twoletstatements as well: multiple bindings at once is nice. As we become more proficient in Rust, we’ll figure out which style is better, but it’s mostly a judgement call.Type annotations
-Most of the time, Rust uses ‘type inference’, meaning that it attempts to infer +
Most of the time, Rust uses type inference, meaning that it attempts to infer the types of your bindings rather than you having to declare them explicitly even though Rust is a statically typed language. Occasionally, Rust won't have enough information to infer the type of your value, and you will need to add a type annotation in with the pattern.
-Here’s what a
+letstatement with a ‘type annotation’ looks like:Here’s what a
letstatement with a type annotation looks like:fn main() { let x: i32 = 5; }