Rust:
```rust
-# use std::boxed::Box;
{
let x = Box::new(5);
}
to a function? Let's look at some code:
```rust
-# use std::boxed::Box;
fn main() {
let x = Box::new(5);
code, where we print out the value of `x`:
```{rust,ignore}
-# use std::boxed::Box;
fn main() {
let x = Box::new(5);
box:
```rust
-# use std::boxed::Box;
fn main() {
let x = Box::new(5);
This function borrows an `i32` from its caller, and then increments it. When
the function is over, and `num` goes out of scope, the borrow is over.
+We have to change our `main` a bit too:
+
+```rust
+fn main() {
+ let mut x = 5;
+
+ add_one(&mut x);
+
+ println!("{}", x);
+}
+
+fn add_one(num: &mut i32) {
+ *num += 1;
+}
+```
+
+We don't need to assign the result of `add_one()` anymore, because it doesn't
+return anything anymore. This is because we're not passing ownership back,
+since we just borrow, not take ownership.
+
# Lifetimes
Lending out a reference to a resource that someone else owns can be
three. The ownership system in Rust does this through a concept called
*lifetimes*, which describe the scope that a reference is valid for.
-Let's look at that function which borrows an `i32` again:
+Remember the function that borrowed an `i32`? Let's look at it again.
```rust
fn add_one(num: &i32) -> i32 {
```rust
fn main() {
- let y = &5; // -+ y goes into scope
+ let y = &5; // -+ y goes into scope
// |
// stuff // |
// |
}
fn main() {
- let y = &5; // -+ y goes into scope
+ let y = &5; // -+ y goes into scope
let f = Foo { x: y }; // -+ f goes into scope
// stuff // |
// |
let x; // -+ x goes into scope
// |
{ // |
- let y = &5; // ---+ y goes into scope
+ let y = &5; // ---+ y goes into scope
let f = Foo { x: y }; // ---+ f goes into scope
x = &f.x; // | | error here
} // ---+ f and y go out of scope