{"id":20180390,"url":"https://github.com/ppcamp/rust-learning","last_synced_at":"2026-06-13T21:33:59.878Z","repository":{"id":114435671,"uuid":"338586946","full_name":"ppcamp/rust-learning","owner":"ppcamp","description":"A repository that will be used to put everything that I've did when I was reading the rust documentation and examples.","archived":false,"fork":false,"pushed_at":"2021-06-16T02:43:58.000Z","size":72,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-10T12:40:18.868Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ppcamp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-13T14:00:28.000Z","updated_at":"2021-06-16T02:44:01.000Z","dependencies_parsed_at":"2023-03-15T12:30:55.562Z","dependency_job_id":null,"html_url":"https://github.com/ppcamp/rust-learning","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ppcamp/rust-learning","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppcamp%2Frust-learning","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppcamp%2Frust-learning/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppcamp%2Frust-learning/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppcamp%2Frust-learning/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ppcamp","download_url":"https://codeload.github.com/ppcamp/rust-learning/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ppcamp%2Frust-learning/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34301731,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-13T02:00:06.617Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T02:30:30.740Z","updated_at":"2026-06-13T21:33:59.860Z","avatar_url":"https://github.com/ppcamp.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\n  \u003cimg\n      src=\"https://doc.rust-lang.org/book/img/ferris/unsafe.svg\"\n      alt=\"Rust mascot\"\n      width=\"70px\"\n      height=\"70px\"\n  /\u003e\n\u003c/p\u003e\n\n# rust-learning\n\nA repository that will be used to put everything that I've did when\nI was reading the rust documentation and examples.\n\n\u003cdiv style=\"clear:both\"\u003e\u003c/div\u003e\n\n\n- [rust-learning](#rust-learning)\n  - [1. Getting started](#1-getting-started)\n  - [2. Programming a Guessing Game](#2-programming-a-guessing-game)\n  - [3. Common programming Concepts](#3-common-programming-concepts)\n  - [4. Understanding Ownership](#4-understanding-ownership)\n  - [5. Using structs to structure related data](#5-using-structs-to-structure-related-data)\n  - [6. Enums and Pattern Matching](#6-enums-and-pattern-matching)\n  - [7. Managing Growing Projects with Packages, Crates, and Modules](#7-managing-growing-projects-with-packages-crates-and-modules)\n  - [8. Common Collections](#8-common-collections)\n  - [9. Error handling](#9-error-handling)\n  - [10. Generic types, traits and lifetimes](#10-generic-types-traits-and-lifetimes)\n  - [11. Writing Automated Tests](#11-writing-automated-tests)\n  - [12. An I/O project: Building a command line program](#12-an-io-project-building-a-command-line-program)\n  - [13.](#13)\n  - [Usefull tips](#usefull-tips)\n    - [Cargo:](#cargo)\n    - [Adding functionalities to cargo](#adding-functionalities-to-cargo)\n\n\n\u003cbr/\u003e\n\u003cbr/\u003e\n\u003cbr/\u003e\n\n## 1. Getting started\n\nIn this section we learn how to install the `rust` env, of course, only the basic. You can find another tips\nin the [cookbook][book-1] , in the [cargo book][cargo-1],\n\n```bash\ncargo check # not build, just check the project for erros\ncargo build # create the exec\ncargo run   # runs the project\n```\n\n\u003cbr/\u003e\n\n## 2. Programming a Guessing Game\n[Reference][book-2]. In this section whe learn:\n   * How to add a new package:\n   * handle with input (similar to Java, you'll need to parse every input made as a char buffer)\n   * rand numbers\n   * match introduction (arrow functions)\n   * error handling\n\n\u003cbr/\u003e\n\n## 3. Common programming Concepts\n\n[Reference][book-3]. In this section we see some of the rust's concepts:\n  - The **let** keyword allows you to change an imutable variable, i.e., you'll create\n    an variable and will destroy the variable used before.\n  - An `constant` it's like an macro in C programming\n  - The basics [types][book-3-dtypes]\n  - The [char][book-3-dtypes-char] rust type\n  - Tuples and its destructuring operation\n  - Rust code uses `snake_case` as the conventional style for function and variable names.\n  - Rust doesn’t care where you define your functions, only that they’re defined somewhere.\n  - Expressions do not include ending semicolons.\n  - [Conditionals][book-3-cf] (if statments)\n  - [Loops][book-3-loops] and `range` type\n\n\n\u003cbr/\u003e\n\n## 4. Understanding Ownership\n\n[Reference][book-4-ownership]. In this chapter we learn how rust handles with memory allocation and \"de-allocation\". Furthermore\nwe also learn:\n  - The equivalent `const char*` in rust\n  - How rust handles with multiples pointers to the same object\n  - How rust [deallocate][book-4-memo] the memory for variables that uses Heap\n  - When you pass some variable allocated in some scope to other function, it will be deleted in \"main\" scope\n  - You can return an tuple\n  - Rust only allows one mutable reference to a specific scope (`{}`), however, you can have multiples immutable references to that scope. This behavior prevents race conditions to this data at a compile time.\n  - The rust blocks the creation of dangling references, it means that you can't create a `reference pointer` to some data in a particular scope that will be freed due to the ownership behavior.\n  - The rust compiler seems to be based on variables scopes, i.e., when you try to do something like this:\n    ```rust\n    // Will raise an error at compile time\n    let pos = first_word(\u0026s);\n    let slice = \u0026s[..pos];\n    s.clear();\n    println!(\"Started at: {} -\u003e {}\", pos, slice);\n\n    // Won't raise an error at compile time (will \"panick\")\n    let pos = first_word(\u0026s);\n    s.clear();\n    println!(\"Started at: {} -\u003e {}\", pos, \u0026s[..pos]);\n    ```\n  - I think that we can't convert the \"sliced\" string to a real one due to the pointer used to, however, we can create another object and use reference of slice type to do so. The slice type are described [here][book-4-slice]\n\n\u003cbr/\u003e\n\n## 5. Using structs to structure related data\n\n[Reference][book-5]. In this section we learn how to create a rust structure (the formatting is quite similar to json objects in js). We also learn:\n  - How define a struct object\n  - How create a struct object using a tuple approach\n  - An example using struct named and unnamed objects\n  - The [debug][book-5-debug] flag. Used to print the objects as a json formatted\n  - Putting a function as in [object oriented][book-5-poo] way method\n  - How rust \"autocompletes\" the [function calling][book-5-dereferencing]\n\n\u003cbr/\u003e\n\n## 6. Enums and Pattern Matching\n[Reference][book-6]. In this chapter we see:\n  - [How to handle][book-6-def] with `enum` type\n  - The [Option\u003cT\u003e object][book-6-object], which contains the Some(val) and None\n  - The [if let statment][book-6-iflet], which can be used to access the enum when you won't used all patterns defined\n  - Acess [internal values in enums][book-6-match]\n  - Enums used as an class type\n\n\u003cbr/\u003e\n\n## 7. Managing Growing Projects with Packages, Crates, and Modules\n[Reference][book-7]. In this chapter we learn how to handle with modules and libraries etc:\n  - The entire module tree is rooted under the implicit module named crate.\n  - An absolute path starts from a crate root by using a crate name or a literal crate\n  - A relative path starts from the current module and uses self, super, or an identifier in the current module.\n  - Rust put all items inside a module(functions, methods, structs, enums, modules, and constants) are private by default. However you can expose using keyword `pub`\n  - You can use relative paths by putting the keyword `super` before the module\n  - The `use` keyword bring the [module][book-7-use] to the current context\n  - When using `enums` or `structs` it's recommended to use the full path-like, otherwise, you could use the `use` statments [to make them simplier][book-7-conventions].\n  - You can use [aliases][book-7-alias] like js lang when imports.\n  - You can re-export modules by putting a `pub` keyword before `use` statment\n  - [External packages][book-7-external]\n  - You can use nested paths, i.e, `use some::package::{self, some}`\n  - You can glob, `use some::*`\n  - The compiler will also look at `folder_module_name/mod.rs`, so this will work if I create a directory `example` containing a file `mod.rs`, see more by clicking [here][ext-1]\n  - You can create a library with cargo using `cargo new --lib lib_name`, if so, you must add it in the same way you'll do when using crate packages, i.e, updating the `Cargo.toml` using a path to specify where to find them. Check it out by clicking [here][ext-2].\n  - When you aren't using library approach you must push the module into scope with mod keyword;\n\n\u003e - Packages: A Cargo feature that lets you build, test, and share crates\n\u003e - Crates: A tree of modules that produces a library or executable\n\u003e - Modules and use: Let you control the organization, scope, and privacy of paths\n\u003e - Paths: A way of naming an item, such as a struct, function, or module\n\n\u003cbr/\u003e\n\n## 8. Common Collections\n[Reference][book-8]. In this section we see the \"std\" equivalent of `C++` for `Rust`. We cover in this section:\n  - [Vectors][book-8-vec] and how to update them\n  - The get method in vector, return us an `Option\u003cT\u003e` type\n  - Ownership when concat strings\n  - Due the [string memory management][book-8-str], you **can't** access string values as in the same way of `C++`, 'cause rust use 2Bytes for each utf-8 char, and index them using byte approach.\n  - Like said in item above, you can't slice using int too, you'll need to use a byte repr way, if so.\n  - [Hash][book-8-hash] will become the owner of the object.\n  - Hash, in generall, overwrite the key.\n  - Hash, by default, it's implemented [focusing in seccurity][book-8-hash-sc]\n\n\u003cbr/\u003e\n\n## 9. Error handling\n[Reference][book-9].\n\u003e Rust groups errors into two major categories: recoverable and unrecoverable errors.\nAnothers infos:\n  - When rust goes to an [unrecoverable error][book-9-error], it will `panic`, and so, it'll backtrace all path to clear memory, by default, however you can change this behavior.\n  - Error [types][book-9-kinds]\n  - `unwrap()` return the `Ok` value, if returned `Err`, will panic!\n  - `expect()` same as `unwrap()`, however, let you to pass a new panic! message.\n  - You can use the `Result` enum to [propagate][book-9-prop] errors (return it to main function or something like that)\n  - [?][book-9-?] mark in rust is used to parse, if Ok, return it to code, if Err, return the err code to main. You only can use it in functions that return `Result\u003cT,E\u003e` type. However, it allows the main function too.\n\n\n\u003cbr/\u003e\n\n## 10. Generic types, traits and lifetimes\n[Reference][book-10]. In this section we see the \"template\" equivalent of `C++`.\n\u003cmark\u003eThis chapter explain one of the most important things about rust working\u003c/mark\u003e\n  - You can use a generic type to simplify functions.\n  - There's a tradeoff in compilation time when using generic methods.\n  - [Traits][book-10-traits] are like `interface` in other langs like *Java* or *Javascript*\n  - To implement a trait, we must define a type and then, using `impl TRAITname for TYPEname`.\n  - To use a trait for another module you must import them too\n  - If we want to force the same type in generic parameters, we must use a [trait bound][book-10-traits-v] (the most verbose way)\n  - We can use [multiples][book-10-multi-traits] implementations for traits\n  - When handling with multiples traits, we can use the [where][book-10-where] clauses\n  - We can return a trait instead of its type implementation\n  - We can set a trait bound in a `impl`. In this approach, the generic trait will work like a [conditional][book-10-cond-trait]\n  - Sometimes, you'll need to specify an lifetime anotation to rust. The [lifetimes][book-10-lifetime] annotations are usefull to prevent your code to go to `trash` values or `null`,\n  - The rust can handle with functions that have one and-only-one parameter, that tooks a reference and return a reference in a function. This is because rust have some codes that are added into compiler. They are named as [lifetime elision rules][book-10-elision]. In another workds, `Rust` will infer the lifetime for references in basic cases.\n  - The `static` lifetime will make the reference to be global in your code.\n\n\u003e The patterns programmed into Rust’s analysis of references are called the lifetime elision rules.\n\n\u003cbr/\u003e\n\n## 11. Writing Automated Tests\n[Reference][book-11]. Rust brings an suite to test your code by default. This feature allows you to check some\nfunction behavior for example.\n  - To create a test, you should add a `test` atribute: `#[test]`\n  - The common practice when handling with [unitary tests][ext-3] is to put them inside the proper modules files.\n  - By default, the tests runs in many threads, however, you can type:\n  ```bash\n  # run tests in single core\n  cargo test -- --test-threads=1\n  ```\n  - You can type:\n  ```bash\n  # to show the second parameter of an assert_eq|ne macro on success cases\n  cargo test -- --show-output\n  # to run tests that matches with this name\n  cargo test one_hundred\n  ```\n  - You can run test over [private][book-11-private] methods too\n  - When you use `#[cfg(test)]` this will isolate your tests from the rest of code, so, the tests will only be builded when requested by `cargo test`\n  - Rust also provides a [\"e2e\" test way][book-11-e2e].\n    - To run e2e tests, the modules must be inside `lib.rs`'s file (otherwise, you can't include them as crate);\n    - The tests must be inside `tests` directory, which is in the root folder, aside with `src`.\n  - You can also, include a crate that will only be used in tests and benchmarks by defining them under `[dev-dependencies]`\n\n\n\n\u003cbr/\u003e\n\n## 12. An I/O project: Building a command line program\n[Reference][book-12]. This chapter is a recap of the many skills you’ve learned so far and an exploration of a few more standard library features.\n  - There's a huge cost when using `clone()` so, most of rustaceans don't use this.\n  - This chapter is very simple, most of work here was already done.\n\n\n\u003cbr/\u003e\n\n## 13.\n\n\n---\n\n## Usefull tips\n\n### Cargo:\n```bash\ncargo build             # build the package\ncargo check             # does the same process that build without building an executable\ncargo clean             # clear the executables (target) directory\ncargo init              # create a new rust package in the current directory\ncargo new               # start a new rust package with a new dir\ncargo new --lib newlib  # create new library named newlib\ncargo r                 # execute the rust executable\ncargo run               # ... the same as above\ncargo search            # similar to pip search\ncargo t                 # use the testing rust package to test the programs\ncargo test              # ... the same as above\ncargo update            # update the packages dependencies in Cargo.toml\n```\n\n### Adding functionalities to cargo\nTuto based on this [site](https://thenewstack.io/tutorial-import-libraries-of-rust-code-with-crates-io/)\n\n```bash\ncargo install cargo-edit  # install a rust binary that will give more power to cargo\n\n# To install this tool you must to add the following packages libraries also\nsudo apt install openssl libssl-dev\n\n# This tool extends Cargo to allow you to add, remove, and upgrade dependencies by modifying your Cargo.toml file from the command line\ncargo add package         # This automatically add a package into Cargo.toml file\ncargo rm package          # This automatically remove a package from Cargo.toml file\ncargo upgrade             # This automatically upgrade all your packages dependencies\n```\n\n\u003c!-- Links ---\u003e\n\n[ext-1]: https://stevedonovan.github.io/rust-gentle-intro/4-modules.html\n\n[ext-2]: https://stackoverflow.com/questions/45519176/how-do-i-use-or-import-a-local-rust-file\n\n[ext-3]: https://stackoverflow.com/a/39009227/10013122\n\n[cargo-1]: https://doc.rust-lang.org/cargo/getting-started/installation.html\n\n[book-1]: https://doc.rust-lang.org/book/ch01-00-getting-started.html\n\n[book-2]: https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html\n\n[book-3]: https://doc.rust-lang.org/book/ch03-00-common-programming-concepts.html\n\n[book-3-dtypes]: https://doc.rust-lang.org/book/ch03-02-data-types.html#scalar-types\n\n[book-3-dtypes-char]: https://doc.rust-lang.org/book/ch03-02-data-types.html#the-character-type\n\n[book-3-cf]: https://doc.rust-lang.org/book/ch03-05-control-flow.html\n\n[book-3-loops]: https://doc.rust-lang.org/book/ch03-05-control-flow.html#repeating-code-with-loop\n\n[book-4-ownership]: https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#ownership-and-functions\n\n[book-4-memo]: https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#memory-and-allocation\n\n[book-4-slice]: https://doc.rust-lang.org/book/ch04-03-slices.html#the-slice-type\n\n[book-5]: https://doc.rust-lang.org/book/ch05-00-structs.html\n\n[book-5-debug]: https://doc.rust-lang.org/book/ch05-02-example-structs.html#adding-useful-functionality-with-derived-traits\n\n[book-5-poo]: https://doc.rust-lang.org/book/ch05-03-method-syntax.html#defining-methods\n\n[book-5-dereferencing]: https://doc.rust-lang.org/book/ch05-03-method-syntax.html#wheres-the---operator\n\n[book-6]: https://doc.rust-lang.org/book/ch06-00-enums.html\n\n[book-6-def]: https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html#defining-an-enum\n\n[book-6-object]: https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html#the-option-enum-and-its-advantages-over-null-values\n\n[book-6-match]: https://doc.rust-lang.org/book/ch06-02-match.html#the-match-control-flow-operator\n\n[book-6-iflet]: https://doc.rust-lang.org/book/ch06-03-if-let.html\n\n[book-7]: https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html\n\n[book-7-use]: https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html\n\n[book-7-conventions]: https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#creating-idiomatic-use-paths\n\n[book-7-alias]: https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#providing-new-names-with-the-as-keyword\n\n[book-7-external]: https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#using-external-packages\n\n[book-8]: https://doc.rust-lang.org/book/ch08-00-common-collections.html\n\n[book-8-vec]: https://doc.rust-lang.org/book/ch08-01-vectors.html\n\n[book-8-str]: https://doc.rust-lang.org/book/ch08-02-strings.html\n\n[book-8-hash]: https://doc.rust-lang.org/book/ch08-03-hash-maps.html#storing-keys-with-associated-values-in-hash-maps\n\n[book-8-hash-sc]: https://doc.rust-lang.org/book/ch08-03-hash-maps.html#hashing-functions\n\n[book-9]: https://doc.rust-lang.org/book/ch09-00-error-handling.html\n\n[book-9-error]: https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html#unwinding-the-stack-or-aborting-in-response-to-a-panic\n\n[book-9-kinds]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#matching-on-different-errors\n\n[book-9-prop]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#propagating-errors\n\n[book-9-?]: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#propagating-errors\n\n[book-10]: https://doc.rust-lang.org/book/ch10-00-generics.html\n\n[book-10-traits]: https://doc.rust-lang.org/book/ch10-02-traits.html\n\n[book-10-traits-v]: https://doc.rust-lang.org/book/ch10-02-traits.html\n\n[book-10-multi-traits]: https://doc.rust-lang.org/book/ch10-02-traits.html#specifying-multiple-trait-bounds-with-the--syntax\n\n[book-10-where]: https://doc.rust-lang.org/book/ch10-02-traits.html#clearer-trait-bounds-with-where-clauses\n\n[book-10-cond-trait]: https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods\n\n[book-10-lifetime]: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-annotations-in-function-signatures\n\n[book-10-elision]: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision\n\n[book-11]: https://doc.rust-lang.org/book/ch11-00-testing.html\n\n[book-11-private]: https://doc.rust-lang.org/book/ch11-03-test-organization.html#testing-private-functions\n\n[book-11-e2e]: https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-directory\n\n[book-12]: https://doc.rust-lang.org/book/ch12-00-an-io-project.html\n\n[book-12-a]: https://doc.rust-lang.org/book/ch12-00-an-io-project.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppcamp%2Frust-learning","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fppcamp%2Frust-learning","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fppcamp%2Frust-learning/lists"}