Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional explanation to the already existing chapters #19

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
227 changes: 188 additions & 39 deletions lessons/en/chapter_1.yaml

Large diffs are not rendered by default.

154 changes: 147 additions & 7 deletions lessons/en/chapter_10.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,150 @@
- title: Chapter 10 - The End
- title: Chapter 10 - I/O
content_markdown: >
It's been a joy to have you on the Tour of Rust. Ferris and the Tour of Rust
team sincerely hope you enjoy the journey ahead! If you
In computing, `I/O` is an abbreviation for `Input/Output` operation.

The `input` is that the computer and tha algorithm receive and
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved
the `output` represents the result generated based on the `input`.

Thing about `I/O` as a stream of information

A compute system without output is nearly useless.

It will always run the same code on the same data and, thus, produce the same result.

have felt comfortable this far, we strongly recommend diving deeper with
these resources:
- title: Do it locally
content_markdown: >
In this chapter, the Playground will be just a code support for you :(.

Since most of the `I/O` programs are designed to compile on a local machine
(yours :) ), consider setting up a Rust environment on your personal computer and
familiarise yourself with the terminal.

Also, consider using a `IDE`, such as `VS Code` or `RustRover`
and familiarise yourself with the [terminal](https://www.youtube.com/watch?v=lZ7Kix9bjPI).

- title: Standard Input (stdin)
content_markdown: >
`Standard Input` refers to the data provided by the user in order for the algorithm
to make something with it.
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved


Thus, the `input` represents what a program is being given.
Mostly, in terms of `input`, you'll work with `String` and `files`.


The Rust library `std::io` has the necessary components to interact with `I/O`
channels, such as the keyboard or any other input source.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=use+std%3A%3Aio%3B%0A%0Afn+main%28%29+%7B%0A++++let+mut+input+%3D+String%3A%3Anew%28%29%3B%0A%0A++++%2F%2F+the+read+will+be+stopped+by+%60%5Cn%60+character%0A++++if+let+Ok%28_%29+%3D+io%3A%3Astdin%28%29.read_line%28%26mut+input%29+%7B%0A++++++++println%21%28%22Input+text+%3A+%7B%7D%22%2C+input%29%3B%0A++++%7D%0A%7D%0A

- title: Standard Output (stdout)
content_markdown: >
Remember the first lesson? Can you notice something relevant to `I/O`?

Of course that `println!` is does an output operation,
in fact it directs (outputs) text to `stdout` (stdout)
and it will be displayed on the screen.

If you don't want to print a new line character `\n` for you, use `print!`
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=use+std%3A%3Aio%3B%0A%0Afn+main%28%29+%7B%0A++++let+mut+input+%3D+String%3A%3Anew%28%29%3B%0A%0A++++%2F%2F+the+read+will+be+stopped+by+%60%5Cn%60+character%0A++++if+let+Ok%28_%29+%3D+io%3A%3Astdin%28%29.read_line%28%26mut+input%29+%7B%0A++++++++println%21%28%22Input+text+%3A+%7B%7D%22%2C+input%29%3B%0A++++%7D%0A%7D%0A

- title: Standard Error (stdout)
content_markdown: >
In order to separate error reporting from common printing, you can use `eprint!` and
`eprintln!` macros that will display text in the standard error (`stderr`) channel,
instead of `stdout`. Use this macro with an informative message.

In UNIX-like systems, such as macOS or LINUX, you can separate the two types of
output by using redirections:
- `./main > output.txt`
- `./main >> output.txt`
- `./main 2> output.txt`
- `./main 2>> output.txt`


> The commands with `2` with copy only the errors generated by the program, and the ones without `2` will discards all error.

> THe command with `>>` will add text at the end of the file, while the operator `>` will override the file.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=use+std%3A%3Aio%3B%0A%0Afn+main%28%29+%7B%0A++++let+mut+input+%3D+String%3A%3Anew%28%29%3B%0A%0A++++%2F%2F+the+read+will+be+stopped+by+%60%5Cn%60+character%0A++++if+let+Ok%28_%29+%3D+io%3A%3Astdin%28%29.read_line%28%26mut+input%29+%7B%0A++++++++println%21%28%22Input+text+%3A+%7B%7D%22%2C+input%29%3B%0A++++%7D%0A%7D%0A

- title: File Descriptors
content_markdown: >
Now that we know what are the basic `I/O` operations, we dive even deeper.

You've already seen before: `stdin` (standard input), `stdout` (standard output) and `stderr` (stand error).
For each of them it is associated a positive integer number, a unique identifier
for an `I/O` channel (example: file), known as a `file descriptor (fd)`.

* [The Official Rust Programming
Book](https://doc.rust-lang.org/stable/book/)
Therefore:
- `stdin`: 0
- `stdout`: 1
- `stderr`: 2
- files

[![file descriptors](https://cs-pub-ro.github.io/operating-systems/assets/images/file-descriptors-d19424f0a417ecd1c032f98a1969ad75.svg)


Working with files plays an important role in the `I/O` operations.

You've already learned to open a text file and read its content.

But how about writing data in it, as an `I/O` channel.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=use+std%3A%3Afs%3A%3A%7Bself%2C+File%7D%3B++++++%2F%2F+file+system%0Ause+std%3A%3Aio%3A%3A%7Bself%2C+Write%7D%3B+++++%2F%2F+input+output%0A%0Afn+main%28%29+-%3E+io%3A%3AResult%3C%28%29%3E+%7B%0A++++let+file_name+%3D+%22output.txt%22%3B%0A%0A++++%2F%2F+creates+the+file+if+it+doesn%27t+already+exists%0A++++let+mut+file+%3D+File%3A%3Acreate%28file_name%29%3F%3B%0A%0A++++let+text_to_write+%3D+%22Hello%2C+World%21%5Cn%5C%0A++++++++++++++++++++++++++++This+is+a+line+of+text.%5Cn%22%3B%0A++++file.write_all%28text_to_write.as_bytes%28%29%29%3F%3B%0A%0A++++let+absolute_path+%3D+fs%3A%3Acanonicalize%28file_name%29%3F%3B%0A++++println%21%28%22Text+has+been+written+to%3A+%7B%3A%3F%7D%22%2C+absolute_path%29%3B%0A%0A++++return+Ok%28%28%29%29%3B%0A%7D%0A

- title: System Arguments
content_markdown: >
A Rust program is able to receive `input` from the in-line arguments.

In order to do so, open a [terminal](https://www.youtube.com/watch?v=lZ7Kix9bjPI), compile it and pass the arguments to the executable in the command promt.

These arguments might be relevant files, flags and so on.
The developer must document their purpose.

If you are using LINUX or macOS, bear in mind these commands:
```bash
$ touch main.rs
$ rustc main.rs
$ ./main.rs 2 3 4 5
```

Otherwise, for Windows environment, on a PowerShell and paste them
```PowerShell
> echo. > main.rs
> rustc main.rs
> .\main.exe 2 3 4 5
```


> Notice that the first argument is the executable itself

code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=use+std%3A%3Aenv%3B+++++++%2F%2F+env+stands+for+environment%0A%0Afn+main%28%29+%7B%0A++++let+args%3A+Vec%3CString%3E+%3D+env%3A%3Aargs%28%29.collect%28%29%3B%0A++++println%21%28%22number+of+arguments+%3D+%7B%7D%22%2C+args.len%28%29%29%3B%0A++++println%21%28%22the+inline+arguments+are+%3A+%7B%3A%3F%7D%22%2C+args%29%3B%0A%7D%0A

- title: Environment Variables
content_markdown: >
You've seen that the Rust standard library grants access to the system.

Using the `std::env` module, you can do in Rust some task that might require
a UNIX terminal, such as:
- command line arguments
- printing the current working directory
- current executable path
- working with environmental variables
- working with files and directories

code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=use+std%3A%3Aenv%3B+++%2F%2F+for+interacting+with+the+system%27s+environment%0A%0Afn+main%28%29+%7B%0A%0A++++if+let+Ok%28current_dir%29+%3D+env%3A%3Acurrent_dir%28%29+%7B%0A++++++++if+let+Some%28pwd%29+%3D+current_dir.to_str%28%29+%7B%0A++++++++++++println%21%28%22The+current+working+directory+%3D+%7B%7D%22%2C+pwd%29%3B%0A++++++++%7D%0A++++%7D%0A%0A++++%2F%2F+%24+echo+%24PWD++++++%23+environment+variable%0A++++%2F%2F+if+you+don%27t+want+to+see+Seme%28...%29%2C+you+have+to+pattern+match+on+Ok%28%29%0A++++println%21%28%22The+current+working+directory+%3D+%7B%3A%3F%7D%22%2C+env%3A%3Avar%28%22PWD%22%29.ok%28%29%29%3B%0A%0A++++%2F%2F+%24+echo+%24PWD++++++%23+environment+variable%0A++++if+let+Ok%28pwd%29+%3D+env%3A%3Avar%28%22PWD%22%29+%7B%0A++++++++println%21%28%22The+current+working+directory+%3D+%7B%7D%22%2C+pwd%29%3B%0A++++%7D%0A%0A++++%2F%2F+%24+echo+%24USER+++++%23+environment+variable%0A++++if+let+Ok%28user%29+%3D+env%3A%3Avar%28%22USER%22%29+%7B%0A++++++++println%21%28%22The+current+user+is%3A+%7B%7D%22%2C+user%29%3B%0A++++%7D%0A%0A%0A++++%2F%2F+%24+echo+%24IDK++++++%23+I+suppose+you+didn%27t+set+this+variable+%3A%29%0A++++if+let+Err%28err%29+%3D+env%3A%3Avar%28%22IDK%22%29+%7B%0A++++++++eprintln%21%28%22IDK+%3A+%7B%7D%22%2C+err%29%3B%0A++++++++env%3A%3Aset_var%28%22IDK%22%2C+%22%3D+I+don%27t+know%22%29%3B%0A++++++++println%21%28%22IDK+%3D+%7B%3A%3F%7D%22%2C+env%3A%3Avar%28%22IDK%22%29.ok%28%29%29%3B%0A++++++++env%3A%3Aremove_var%28%22IDK%22%29%3B%0A++++%7D%0A%7D%0A
- title: Chapter 11 - Conclusion
content_markdown: >
Now that you know the basic of Input/Output operations, you used just two
Rust libraries along the way, and they are standard, btw: `std::env` and `std::fs`.
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved

Now, you can build your own file managing system, Web App or even API.

Checkout these resources:
- [Environment Variables](https://youtu.be/npsMN-tZNVs)
- [Rust API](https://youtu.be/_ccDqRTx-JU)

2 changes: 1 addition & 1 deletion lessons/en/chapter_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

and including an end number.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20for%20x%20in%200..5%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20for%20x%20in%200..%3D5%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20x)%3B%0A%20%20%20%20%7D%0A%7D%0A
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A%0A++++for+x+in+0..5+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+x%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++for+x+in+0..%3D5+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+x%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++let+nums+%3A+%5Bi32%3B+5%5D+%3D+%5B10%2C+-1%2C+9%2C+-80%2C+1%5D%3B%0A++++%0A++++print%21%28%22%7B%7D+%3A+%22%2C+stringify%21%28nums%29%29%3B%0A++++for+el+in+nums+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+el%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%0A++++print%21%28%22%7B%7D+%3A+%22%2C+stringify%21%28nums%29%29%3B%0A++++for+idx+in+0+..+nums.len%28%29+%7B%0A++++++++print%21%28%22%7B%7D+%22%2C+nums%5Bidx%5D%29%3B%0A++++%7D%0A++++println%21%28%29%3B%0A%7D%0A
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved
- title: match
content_markdown: >
Miss your switch statement? Rust has an incredibly useful keyword
Expand Down
11 changes: 11 additions & 0 deletions lessons/en/chapter_4.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@
Be a good rustacean and properly use `match` when you can!
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20do_something_that_might_fail(i%3A%20i32)%20-%3E%20Result%3Cf32%2C%20String%3E%20%7B%0A%20%20%20%20if%20i%20%3D%3D%2042%20%7B%0A%20%20%20%20%20%20%20%20Ok(13.0)%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20Err(String%3A%3Afrom(%22this%20is%20not%20the%20right%20number%22))%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20-%3E%20Result%3C()%2C%20String%3E%20%7B%0A%20%20%20%20%2F%2F%20concise%20but%20assumptive%20and%20gets%20ugly%20fast%0A%20%20%20%20let%20v%20%3D%20do_something_that_might_fail(42).unwrap()%3B%0A%20%20%20%20println!(%22found%20%7B%7D%22%2C%20v)%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20this%20will%20panic!%0A%20%20%20%20let%20v%20%3D%20do_something_that_might_fail(1).unwrap()%3B%0A%20%20%20%20println!(%22found%20%7B%7D%22%2C%20v)%3B%0A%20%20%20%20%0A%20%20%20%20Ok(())%0A%7D%0A
- title: About panic!
content_markdown: >
In Rust, `panic!` is a macro used to stop the execution of the program
without a recoverable error. When a panic occurs, the program immediately
stop, unwinding the stack and cleaning up resources along the way.
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved

Moreover, the code instructions written after `panic!` will no longer be executed.

Usually, `panics` can by avoided by pattern matching the error with `Result`.
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++println%21%28%22Reachable.%22%29%3B%0A%0A++++%2F%2F+Generates+a+panic%0A++++panic%21%28%22This+is+a+panic%21%22%29%3B%0A%0A++++%2F%2F+This+line+be+executed%0A++++println%21%28%22Unreachable%22%29%3B%0A%7D%0A
- title: Vectors
content_markdown: >
Some of the most useful generic types are collection types. A vector is a
Expand Down
33 changes: 33 additions & 0 deletions lessons/en/chapter_7.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@

That said, Rust implements many programming language features, so that you
might not mind this lacking.
- title: self and Self
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=struct+Coordinates+%7B%0A++++x%3A+i32%2C%0A++++y%3A+i32%2C%0A%7D%0A%0Aimpl+Coordinates+%7B%0A++++%2F%2F+the+return+value%2C+Self%2C+has+the+data+type+as+the+implemented+struct+%0A++++%2F%2F+this+method+is+static%0A++++fn+new%28xval%3A+i32%2C+yval%3A+i32%29+-%3E+Self+%7B%0A++++++++return+Coordinates+%7B%0A++++++++++++x%3A+xval%2C%0A++++++++++++y%3A+yval%2C%0A++++++++%7D%0A++++%7D%0A++++%0A++++fn+setx%28%26mut+self%2C+xval%3A+i32%29+%7B%0A++++++++self.x+%3D+xval%3B%0A++++%7D%0A++++%0A++++fn+sety%28%26mut+self%2C+yval%3A+i32%29+%7B%0A++++++++self.y+%3D+yval%3B%0A++++%7D%0A++++%0A++++fn+disp%28%26self%29+%7B%0A++++++++println%21%28%22Point+located+at+%3A+%7B%7D+on+OX++%7B%7D+on+OY%22%2C%0A++++++++++++++++self.x%2C+self.y%29%3B%0A++++%7D%0A++++%0A%7D%0A%0Afn+main%28%29+%7B%0A++++let+mut+p+%3D+Coordinates%3A%3Anew%28-1i32%2C+1i32%29%3B%0A++++p.disp%28%29%3B%0A++++p.setx%2810++as+i32%29%3B%0A++++p.sety%28-10+as+i32%29%3B%0A++++p.disp%28%29%3B%0A%7D%0A
content_markdown: >
In Rust, `self` and `Self` are fundamental concepts in ownership and
borrowing system. Both of them are used in `impl` and `trait` blocks.

`self`:
- refers to an **instance of struct**
- reference to itself
- is the first parameter of the method
- method conataining the `self` parameter are called using the name of the instance, the operator `.` and the name of the method
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved


`Self`:
- refers to **return type** of the method
- has the same data type as the `struct` that is implemented in a `impl` block
- can be used for static method using the operator `::` (static method do not depend on a instance of a struct and a static method does not contain `self` as parameter)
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved
- title: Defining a macro
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=struct+Coordinates+%7B%0A++++x%3A+i32%2C%0A++++y%3A+i32%2C%0A%7D%0A%0Aimpl+Coordinates+%7B%0A++++%2F%2F+original+copy+constructor%2C+a+static+method%0A++++fn+new%28xval%3A+i32%2C+yval%3A+i32%29+-%3E+Self+%7B%0A++++++++Coordinates+%7B+x%3A+xval%2C+y%3A+yval+%7D%0A++++%7D%0A%7D%0A%0Aimpl+Coordinates+%7B%0A++++fn+disp%28%26self%29+%7B%0A++++++++println%21%28%22Point+located+at%3A+%7B%7D+on+OX%2C+%7B%7D+on+OY%22%2C+self.x%2C+self.y%29%3B%0A++++%7D%0A%7D%0A%0A%2F%2F+the+coord%21+macro%0Amacro_rules%21+coord+%7B%0A++++%28%24x%3Aexpr%2C+%24y%3Aexpr%29+%3D%3E+%7B%0A++++++++Coordinates%3A%3Anew%28%24x%2C+%24y%29%0A++++%7D%3B%0A%7D%0A%0Afn+main%28%29+%7B%0A++++%2F%2F+Use+the+coord%21+macro+to+create+a+new+Coordinates+instance%0A++++let+p1%3A+Coordinates+%3D+coord%21%281%2C+2%29%3B%0A%0A++++%2F%2F+Alternatively%2C+you+can+use+the+coord%21+macro+with+square+brackets%0A++++let+p2+%3D+coord%21%5B2%2C+2%5D%3B%0A++++%0A++++let+p3+%3D+Coordinates+%7B%0A++++++++x%3A+10i32%2C%0A++++++++y%3A+11+as+i32%2C%0A++++%7D%3B%0A++++%0A++++let+p4+%3D+Coordinates%3A%3Anew%28-10%2C+11%29%3B%0A%0A++++p1.disp%28%29%3B%0A++++p2.disp%28%29%3B%0A++++p3.disp%28%29%3B%0A++++p4.disp%28%29%3B%0A%7D%0A
content_markdown: >
We talked before that we can use `macros` to simplify abstractions of our code.

Let us define a `macro` that has the functionality of a copy constructor
for a struct.

Notice that we group the macro's parameters using either `()` or `[]`.

The choice between using `()` and `[]` for `macro` invocations in Rust
often depends on the expected syntax and visual aesthetics. While `()` is more commonly associated with function calls and grouping expressions, `[]` is often associated with array indexing or indexing-like operations.


- title: Encapsulation With Methods
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=struct%20SeaCreature%20%7B%0A%20%20%20%20noise%3A%20String%2C%0A%7D%0A%0Aimpl%20SeaCreature%20%7B%0A%20%20%20%20fn%20get_sound(%26self)%20-%3E%20%26str%20%7B%0A%20%20%20%20%20%20%20%20%26self.noise%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20creature%20%3D%20SeaCreature%20%7B%0A%20%20%20%20%20%20%20%20noise%3A%20String%3A%3Afrom(%22blub%22)%2C%0A%20%20%20%20%7D%3B%0A%20%20%20%20println!(%22%7B%7D%22%2C%20creature.get_sound())%3B%0A%7D%0A
Expand Down
49 changes: 49 additions & 0 deletions lessons/en/chapter_8.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,55 @@
will validate the lifetime of references doesn't last longer than what

it refers to (otherwise we'd get an error when we used it!).
- title: What is a pointer?
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code=fn+main%28%29+%7B%0A++++let+a%3A+i32+%3D+12i32%3B%0A++++let+b%3A+%26i32+%3D+%26a%3B+++%2F%2F+pointer+to+i32%0A++++let+c%3A+%26%26i32+%3D+%26b%3B++%2F%2F+pointer+to+pointer+to+i32%0A++++let+d%3A+%26%26%26i32+%3D+%26c%3B%09%2F%2F+pointer+to+pointer+to+pointer+to+i32%0A++++%0A++++println%21%28%22%7B%7D+%7B%7D+%7B%7D+%7B%7D%22%2C+a%2C+b%2C+c%2C+d%29%3B%0A++++println%21%28%22%7B%7D+%7B%7D+%7B%7D+%7B%7D%22%2C+a%2C+*b%2C+**c%2C+***d%29%3B%0A%7D%0A
content_markdown: >
A pointer is a variable.
TrifanBogdan24 marked this conversation as resolved.
Show resolved Hide resolved

Each variable:
- has a name
- has a data type
- has an address (in the RAM memory)
- stores an value

Why do use data types?

By using data types, we basically tell the CPU how much memory to
allocate for the variables we declare. For instance, the compiler
allocates 8 bytes for each `i8` and `u8` variable. Without pointers, a variables stores a `value`.

You've seen `u16`, `f64`, `usize`. These are data types.
So are `&i32`, `&&i32` `&&&i32` and so on.

The same logic applies to pointers. A pointer is a data type itself and
when we use pointer, the variables stores the `address` of the variable
instead of `value`. For pointer, the stored `value` is an `address`.


| variable name | data type | address | stored value |
| ------------- | --------- | ------- | --------------- |
| a | i32 | 0x100 | 12 |
| b = &a | &i32 | 0x132 | 0x100 (addr) |
| c = &b | &&i32 | 0x2a1 | 0x132 (addr) |
| d = &c | &&&i32 | 0x712 | 0x2a1 (addr) |

> The CPU chooses the address of a variable at runtime
> Do not expect to have the same address at different executions

All variables `i32`, `&i32`, `&&i32`, `&&&i32` are 32-bit variable.

`i32` -> stores a concrete value
`&...&i32` -> stores an address


However, when you try to print references (like `&i32`, `&&i32`, `&&&i32`),
Rust automatically `dereferences` them when using the `{}` format specifier in a `println!` `macro`. This means that the `values` themselves are
printed, not their `addresses`.

[![Pointer](https://i.redd.it/swszegbgpbr71.jpg)
This is for `C`.
For Rust, think that `int` is `u128`, and `*` is Rust's `&`.
- title: Raw Pointers
code: >-
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20a%20%3D%2042%3B%0A%20%20%20%20let%20memory_location%20%3D%20%26a%20as%20*const%20i32%20as%20usize%3B%0A%20%20%20%20println!(%22Data%20is%20here%20%7B%7D%22%2C%20memory_location)%3B%0A%7D%0A
Expand Down
Loading