forked from richardanaya/tour_of_rust
-
Notifications
You must be signed in to change notification settings - Fork 17
Input and Output #20
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
Open
TrifanBogdan24
wants to merge
13
commits into
UPB-CS-OpenSourceUpstream:master
Choose a base branch
from
TrifanBogdan24:new-chapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Input and Output #20
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4f1b081
added explanations
TrifanBogdan24 1fec16e
more about address
TrifanBogdan24 abd3cb7
more about pointers
TrifanBogdan24 58564b3
I/O
TrifanBogdan24 99b1ec8
added chapter : I/O
TrifanBogdan24 d0d2430
added chapter: I/O
TrifanBogdan24 13714bc
fixed typos in chapter 10
TrifanBogdan24 d865564
chapter 10 in RO (Romanian)
TrifanBogdan24 1e68fa5
added \n at the end of RO files
TrifanBogdan24 45e0945
chapter 11 : final conclusion
TrifanBogdan24 73e36d7
fixed typos chapter 10 RO, EN
TrifanBogdan24 78f15a5
chapter 11 is the final one
TrifanBogdan24 118bb3f
Merge branch 'master' into new-chapter
TrifanBogdan24 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,149 @@ | ||
| - 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 what the computer and the algorithm receive and | ||
| 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 an `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 for the algorithm to process. | ||
|
|
||
|
|
||
| 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 what `println!` does is 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 the `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` will copy only the errors generated by the program, and the ones without `2` will discards all errors. | ||
|
|
||
| > 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` (standard 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 | ||
|
|
||
| [ | ||
|
|
||
|
|
||
| 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. | ||
TrifanBogdan24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. | ||
TrifanBogdan24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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 | ||
TrifanBogdan24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```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 | ||
TrifanBogdan24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| a UNIX terminal, such as: | ||
| - command line arguments | ||
| - printing the current working directory | ||
| - current executable path | ||
| - working with environmental variables | ||
TrifanBogdan24 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - 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 10 - 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, by the way: `std::env` and `std::fs`. | ||
|
|
||
| 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) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| - title: Chapter 11 - The End | ||
| 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 | ||
|
|
||
| have felt comfortable this far, we strongly recommend diving deeper with | ||
| these resources: | ||
|
|
||
| * [The Official Rust Programming Book](https://doc.rust-lang.org/stable/book/) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| - title: Capítulo 10 - Fin | ||
| - title: Capítulo 11 - Fin | ||
| content_markdown: > | ||
| Esto es todo por ahora, pero aún quedan más capítulos, así que ¡muy atento! | ||
| Esperamos que disfrutes del viaje. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| - title: Capitul 10 - Fine | ||
| - title: Capitul 11 - Fine | ||
| content_markdown: > | ||
| To es omnicos por nu. Plu tard va venir nov contenete. Yo espera que tu va | ||
| juir li viage a sequer! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| - title: 第 10 章 - 終わりに | ||
| - title: 第 11 章 - 終わりに | ||
| content_markdown: "Rustツアーに参加していただきありがとうございました。フェリスとRustツアーチームは皆さんが今後も楽しんでくれるよう願っています。\t\nここまで楽しんでいただけたなら、以下の資料を通してより深く学ぶのを推奨します。\t\n* [The Official Rust Programming Book](https://doc.rust-lang.org/stable/book/)\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| - title: Capítulo 10 - Fim | ||
| - title: Capítulo 11 - Fim | ||
| content_markdown: | | ||
| Isso é tudo por enquanto. Fique ligado para novos conteúdos. Espero que se divirta nesta jornada! |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.