[package]
name = "hello_world"
-version = "0.1.0"
+version = "0.0.1"
authors = [ "Your name <you@example.com>" ]
[[bin]]
```{notrust,ignore}
$ cargo run
- Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
+ Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
Running `target/guessing_game`
Hello, world!
```
```{notrust,ignore}
$ cargo run
- Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
+ Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
Running `target/guessing_game`
Guess the number!
The secret number is: 7
```{notrust,ignore}
$ cargo run
- Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
+ Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
Running `target/guessing_game`
Guess the number!
The secret number is: 57
```{notrust,ignore}
$ cargo run
- Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
+ Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
Running `target/guessing_game`
Guess the number!
The secret number is: 17
```{notrust,ignore}
$ cargo run
- Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
+ Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
Running `target/guessing_game`
Guess the number!
The secret number is: 58
```{notrust,ignore}
$ cargo run
- Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
+ Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
Running `target/guessing_game`
Guess the number!
The secret number is: 59
```{notrust,ignore}
$ cargo run
- Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
+ Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
Running `target/guessing_game`
Guess the number!
The secret number is: 61
Let's double check our work by compiling:
-```{bash,ignore}
-$ cargo build
+```{bash,notrust}
+$ cargo run
Compiling modules v0.0.1 (file:///home/you/projects/modules)
-$ ./target/modules
+ Running `target/modules`
Hello, world!
```
Rust provides six attributes to indicate the stability level of various
parts of your library. The six levels are:
-* deprecated: this item should no longer be used. No guarantee of backwards
+* deprecated: This item should no longer be used. No guarantee of backwards
compatibility.
* experimental: This item was only recently introduced or is otherwise in a
state of flux. It may change significantly, or even be removed. No guarantee
Rust can't find this function. That makes sense, as we didn't write it yet!
-In order to share this codes with our tests, we'll need to make a library crate.
+In order to share this code with our tests, we'll need to make a library crate.
This is also just good software design: as we mentioned before, it's a good idea
to put most of your functionality into a library crate, and have your executable
crate use that library. This allows for code re-use.
deallocated, leaving it up to your runtime can make this difficult.
Rust chooses a different path, and that path is called **ownership**. Any
-binding that creates a resource is the **owner** of that resource. Being an
-owner gives you three privileges, with two restrictions:
+binding that creates a resource is the **owner** of that resource.
+
+Being an owner affords you some privileges:
1. You control when that resource is deallocated.
2. You may lend that resource, immutably, to as many borrowers as you'd like.
-3. You may lend that resource, mutably, to a single borrower. **BUT**
-4. Once you've done so, you may not also lend it out otherwise, mutably or
- immutably.
-5. You may not lend it out mutably if you're currently lending it to someone.
+3. You may lend that resource, mutably, to a single borrower.
+
+But it also comes with some restrictions:
+
+1. If someone is borrowing your resource (either mutably or immutably), you may
+ not mutate the resource or mutably lend it to someone.
+2. If someone is mutably borrowing your resource, you may not lend it out at
+ all (mutably or immutably) or access it in any way.
What's up with all this 'lending' and 'borrowing'? When you allocate memory,
you get a pointer to that memory. This pointer allows you to manipulate said
# Unsafe
-Finally, there's one more concept that you should be aware in Rust: `unsafe`.
+Finally, there's one more Rust concept that you should be aware of: `unsafe`.
There are two circumstances where Rust's safety provisions don't work well.
The first is when interfacing with C code, and the second is when building
certain kinds of abstractions.