# Describe and explain the importance of the following Move, ownership, lifetimes, borrow ?
## Move
`move` keyword always captured any variable and its value by referance and mutable referance.
`move` can also implement in `Fn` and `FnMut`
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7b657cae512efd2dbe0e1a63c7e5208c)
```rust=1.58.1
// Box return type moves function from stack to heap
fn add(a: i32) -> Box<dyn Fn(i32) -> i32> {
// 'move' applies move semantics to a, so it can outlive this function call
Box::new(move |b| a + b)
}
fn main {
println!("3 + 4 = {}" add(3)(4));
}
```
Error code:
To take a ownership of variable
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8c20ecfab1b80d1efdcdea1ae5b84f4d)
```rust=1.58.1
fn main() {
let a = vec![1, 2, 3];
// - move occurs because `a` has type `Vec<i32>`, which does not implement the `Copy` trait.
let equal_to_a = move |vec| vec == a
//- variable moved due to use in closure
println!("can't use x here: {:?}", a);
//-^ value borrowed here after move
let vec_to_compare = vec![1, 2, 3];
assert!(equal_to_a(vec_to_compare));
}
```
move keyword implement until it's clone
## Ownership
All value in rust have one owner. The owner is responsible for dropping value when it goes out of scope, only way to move the ownership of the value. The owner of the value give away reference to it by letting other pieces of code borrow that value.
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8898b63cae8ec6b5b2f337993d281cac)
```rust=1.58.1
fn main() {
let owned = String::from("hello");
// since we own the value, we may let other variables borrow it
let _immutable_borrow1 = &owned;
// as all current borrows are immutable, we can allow many of them
let immutable_borrow2 = &owned;
// in fact, if we have an immutable reference, we are also free to
// duplicate that reference, since we maintain the invariant that
// there are only immutable references
let _immutable_borrow3 = &*immutable_borrow2;
}
```
Error code
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a19f36c090d24969e4ebacd404fa31bb)
```rust=1.58.1
struct Num {}
fn foo(val: &Num) {}
fn bar(val: &Num) {}
fn main() {
let a = Num{};
let ref_to_val: &Num = &a;
bar(a);
//^
//|expected `&Num`, found struct `Num`
//help: consider borrowing here: `&a`
foo(ref_to_val);
}
```
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=59bb620ee50ae0f70f0605f0b713fa51)
Here `a` take ownership
```rust=1.58.1
struct Num {}
fn foo(val: &Num) {}
fn bar(val: &Num) {}
fn main() {
let a = Num{};
let ref_to_val: &Num = &a;
bar(&a);
foo(ref_to_val);
}
```
## Lifetime
Lifetime in rust programs don't free memory explicitly. Instead, a garbage collector frees memory that will no longer be used.
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b83fbe22e8a2982339192c45ddccffe4)
```rust=1.58.1
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}
```
Error code:
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=aaf78a663ea7461e318a677c2021da07)
```rust=1.58.1
fn main() {
{
let r;
{
let x = 5;
r = &x;
}
println!("r: {}", r);
}
}
```
It clearly shows x does not live long enough to remain valid after the scope ends.
## Borrow
Borrow is assigned to one variable at a time
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f8f73ca466a2a50c0fe164223e67c0bb)
```rust=1.58.1
fn main() {
let a = String::from("hello");
let b = &a;
println!("{}", a);
println!("{}", b);
}
```
Error code:
[play.rust-lang.org](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ef0431403b59b02a350cd20961641147)
```rust=1.58.1
fn main() {
let a = String::from("Hello");
let _b = a;
println!("{}", a);
// ^ value borrowed and moved
}
```
Here error out Use of moved value `a`. when variables go out of scope, the memory that refer to is deallocated–there is no garbage collection. In order to maintain this behavior, and to prevent invalid memory access, there can only be one “owner” of data at a time. In this example, `a` is the original owner, and then `b`becomes the owner.
| Column 3 | Column 1 | Column 2 |
| -------- | -------- | -------- |
| Text | Text | Text |