{"id":18176980,"url":"https://github.com/sahilrajput03/learning_rust","last_synced_at":"2025-07-10T11:09:29.268Z","repository":{"id":109133410,"uuid":"338632748","full_name":"sahilrajput03/learning_rust","owner":"sahilrajput03","description":null,"archived":false,"fork":false,"pushed_at":"2022-02-15T14:48:14.000Z","size":3614,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T10:53:53.663Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sahilrajput03.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-13T17:46:06.000Z","updated_at":"2022-01-05T18:17:29.000Z","dependencies_parsed_at":"2023-04-08T10:38:23.261Z","dependency_job_id":null,"html_url":"https://github.com/sahilrajput03/learning_rust","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sahilrajput03/learning_rust","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sahilrajput03","download_url":"https://codeload.github.com/sahilrajput03/learning_rust/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sahilrajput03%2Flearning_rust/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264569460,"owners_count":23629604,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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-02T17:12:00.047Z","updated_at":"2025-07-10T11:09:29.206Z","avatar_url":"https://github.com/sahilrajput03.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Readme\r\n\r\nLAST UPDATE: Continue doing `rustlings/exercies/errors-handling/README.md`\r\n(it has links which I read last). Learning how to handle errors is\r\nreally awesome, i.e.,\r\n\r\n- in `errors2.rs` test I learned that with return\r\nvalue as `Result\u003ci32, \u0026str\u003e` I am saying that i can return two values\r\ni.e., `Ok(i32)` or `Err(\"a string slice\")`.\r\n\r\n- in `errors3.rs` test I learned that I must return a `Result\u003ca,b\u003e` type\r\n  from the `main()` function so that I can use `?`, which is basically\r\n  does like \"stop the function in runtime when an error is caused\".\r\n\r\n- in `errors4.rs` test I learned that I can define an enum like \r\n\r\n```\r\nenum CreationError {\r\n\tis_negative,\r\n\tis_zero,\r\n}\r\n```\r\n\r\nwhich help me to define like return type as `Result\u003ca,CreationError\u003e` so\r\nthat means I can make use of different types of error by defining the\r\nreturn type for error as `CreationError` as `Result\u003ca, CreationError\u003e`.\r\n\r\n- in `errors5.rs` test I learned that I can make use of somthing\r\n  like `Result\u003c(), Box\u003cdyn error::Error\u003e\u003e` so that I can make a\r\n  explicit list of error which aren't catchable distinguishly but on a\r\n  general basis i.e., any `Err(..)` can be returned for which I have\r\n  implemented `error:Error`. Yo!! This can be hacky way to catch a list\r\n  of errors my adding `error::Error` implementations to them and making\r\n  those errors compatible with the compiler but it isn't good for\r\n  library coz we need to get what exact error is thrown in each case\r\n  which is not possible with `Result\u003ca,CreationError\u003e`.\r\n- in `errors6.rs` test I learned that I can remodel any error to\r\n  anything with a functional approach that is so awesome and feels like\r\n  fucntional approach is good way to simply map the error to our own\r\n  error made up instances, e.g., (from docs).\r\n\r\n  ```\r\n  // LEARN: from docs of map_err:: ~Sahil\r\n  //\r\n  // fn stringify(x: u32) -\u003e String { format!(\"error code: {}\", x) }\r\n  \r\n  // let x: Result\u003cu32, u32\u003e = Ok(2);\r\n  // assert_eq!(x.map_err(stringify), Ok(2));\r\n  \r\n  // let x: Result\u003cu32, u32\u003e = Err(13);\r\n  // assert_eq!(x.map_err(stringify), Err(\"error code: 13\".to_string()));\r\n  ```\r\n\r\n***\r\n\r\n**Try out:**\r\n\r\n- cargo tree: https://doc.rust-lang.org/cargo/commands/cargo-tree.html\r\n- cargo outdate: https://crates.io/crates/cargo-outdated/0.10.2/dependencies\r\n\r\n\r\n## IMPORTANT: Attributes can be crate level (i.e., once for the complete file ~Sahil) or it can be particular to a struct, module, function, etc.\r\n\r\nYou can either:\r\n\r\nSource: https://stackoverflow.com/a/25877389/10012446\r\n\r\n- Add an allow attribute on a struct, module, function, etc.:\r\n\r\n```rs\r\n#[allow(dead_code)]\r\nstruct SemanticDirection;\r\n// So dead_code attibute will only apply to this struct only.\r\n```\r\n\r\n- **Add a crate-level allow attribute; notice the `!`**: `#![allow(dead_code, unused)]`\r\n\r\n**FYI:**: `allow(unused)` will supress all nested warning as well like: `allow(unused_mut, unused_variables, ... and many more..)`.\r\n\r\n- Pass it to rustc: `rustc -A dead_code main.rs`\r\n\r\n- Pass it using cargo via the RUSTFLAGS environment variable: `RUSTFLAGS=\"$RUSTFLAGS -A dead_code\" cargo build`\r\n\r\n## Creating new project:\r\n\r\n```bash\r\ncargo init --bin my-project\r\n# or\r\ncargo init --lib my-library\r\n```\r\n\r\n**more -\u003e global installs**\r\n\r\n- `cargo-edit` : https://crates.io/crates/cargo-edit\r\n\r\n```bash\r\ncargo install cargo-edit\r\n\r\n# Usage:\r\ncargo add pkg-name1 pkg-name2 ... # It wil add latest version of package with its version to your cargo.toml file automatically.\r\n```\r\n\r\n- `cargo-whatfeatures`: https://crates.io/crates/cargo-whatfeatures\r\n\r\n```bash\r\ncargo install cargo-whatfeatures\r\n\r\n# Usage:\r\ncargo whatfeatures package-name-here # Shows you the available features from a package.\r\n```\r\n\r\n- `cargo install` docs:\r\n\r\n```bash\r\ncargo install [options] crate...\r\ncargo install [options] --path path\r\ncargo install [options] --git url [crate...] # This one is useful!\r\ncargo install [options] --list\r\n\r\n# FYI: ~sahil i used it to do:\r\ncargo install --git https://github.com/davidpdrsn/cargo-docserver\r\n```\r\n\r\n**a static http files server with rust**: https://github.com/DenisKolodin/static-server\r\n\r\n**tokio is most trusted and used web framework in rust**\r\n\r\n- [] tokio: https://crates.io/crates/tokio (14.7k stars@github) (this is used in most libraries for providing non-blocking i/o platoform for writing asynchronous operations)\r\n- [] hyper: https://crates.io/crates/hyper (9k stars@github)\r\n- [] async-std: https://crates.io/crates/async-std (3.1k stars@github) (this is similar library to tokio IMO ~Sahil.)\r\n\r\n## todo:\r\n\r\nRust's blog: [here](https://blog.rust-lang.org/)\r\n\r\nRust 2020 survey result [here](https://blog.rust-lang.org/2020/12/16/rust-survey-2020.html).\r\n\r\nDo check if 2021 survey result is launched or not...\r\n\r\n## mongodb resources\r\n\r\n[Official Mongodb Driver for rust](https://docs.mongodb.com/drivers/rust/), [Mongo rust driver@Github](https://github.com/mongodb/mongo-rust-driver/#windows-dns-note)\r\n\r\n[Google - Search - Mongodb for rust](https://www.google.com/search?q=mongodb+with+rust\u0026oq=mongodb+with+rust\u0026aqs=chrome..69i57j0i67i433j35i39j0i433i512j0i67j0i433i512j69i65l2.2221j0j1\u0026sourceid=chrome\u0026ie=UTF-8)\r\n\r\nLearn rust - https://www.rust-lang.org/learn\r\n\r\n## IMPORTANT \u003e\u003e NOW \u003c\u003c\u003e\u003e \u003e \u003c\u003c \u003e\u003e\r\n\r\nContinue book rust programming with example from pg. 129.\r\n\r\n## Install cargo-watch to watch over a project\r\n\r\nCargo installs all binaries to `~/.cargo/bin` directory as state in [docs here](https://doc.rust-lang.org/cargo/commands/cargo-install.html#description).\r\nInstall: `ls ~/.cargo/bin/`\r\n\r\nCrate: https://crates.io/crates/cargo-watch, Docs: https://docs.rs/crate/cargo-watch/7.0.2\r\n\r\nUsage:\r\n\r\n````bash\r\n#Watch for ``cargo run``\r\n$ cargo watch -x run\r\n$ cargo-watch -x run # Notice the dash between cargo and watch.\r\n$ cw # My personal alias for cargo watch i.e., ```cargo watch -x run```\r\n\r\n#Watch for ``cargo check``\r\n$ cargo watch # Default: `cargo check` i.e: cargo watch -x check\r\n\r\n# PRO TIP:\r\ncargo watch -q -c -x run\r\n# Here -c or --clear clears the output before each run.\r\n# -q or --quiet hides the output.\r\n#Look for more command in Crate docs.\r\n\r\n## MY PERSONAL ALIASES FROM .bashrc file:\r\nalias cw='cargo watch -q -c -x \"run -q\"'\r\n#cargo watch --quiet --clear --exec 'run --quiet'\r\n````\r\n\r\n## Primitive types\r\n\r\nhttps://doc.rust-lang.org/std/index.html#primitives\r\n\r\n**Data types - Chapter in officila book**: https://doc.rust-lang.org/book/ch03-02-data-types.html#integer-types\r\n\r\n##### `isize` and `usize`:\r\n\r\nThe isize and usize types depend on the kind of computer your program is running on: 64 bits if you’re on a 64-bit architecture and 32 bits if you’re on a 32-bit architecture.\r\n\r\n## Operators\r\n\r\nhttps://doc.rust-lang.org/book/appendix-02-operators.html#operators\r\n\r\n## signed integer [read more here](topic-signedUnsigned.md)\r\n\r\n## println! is a macro, what else is macro in rust?\r\n\r\nhttps://doc.rust-lang.org/std/index.html#macros\r\n\r\n## Official Rust book\r\n\r\nhttps://doc.rust-lang.org/book/\r\n\r\n## Amazing rustlings game course\r\n\r\nhttps://github.com/rust-lang/rustlings/\r\n\r\n**Install**: [Manual Install@RustlingsRepo](https://github.com/rust-lang/rustlings/#manually)\r\n\r\n**SELF NOTES:: Installed in this repo in `rustlings` folder.**\r\n\r\n**USE rustlings cli tool from the rustlings folder to start the game.**\r\n\r\n## Amazing rust by example book\r\n\r\nhttps://doc.rust-lang.org/stable/rust-by-example/\r\n\r\nAnother awesome book with name \"Rust programming by example ~ Guillaume Gomez and Antoni Boucher\"\r\n\r\n## Comprehensive guide to the Rust standard library APIs.\r\n\r\n(amazing glossary of things in rust ~sahil)\r\n\r\nhttps://doc.rust-lang.org/std/index.html\r\n\r\n(the standard library)\r\n\r\nsrc: https://www.rust-lang.org/learn\r\n\r\n## Guide to editions of rust\r\n\r\nhttps://doc.rust-lang.org/edition-guide/index.html\r\n\r\n(edition guide)\r\n\r\nsrc: https://www.rust-lang.org/learn\r\n\r\n## A book on Rust’s package manager and build system - Cargo\r\n\r\nhttps://doc.rust-lang.org/cargo/index.html\r\n\r\n(cargo book)\r\n\r\nsrc: https://www.rust-lang.org/learn\r\n\r\n## Learn how to make awesome documentation for your crate.\r\n\r\nhttps://doc.rust-lang.org/rustdoc/index.html\r\n\r\n(rustdoc book)\r\n\r\nsrc: https://www.rust-lang.org/learn\r\n\r\n## Familiarize yourself with the knobs available in the Rust compiler.\r\n\r\nhttps://doc.rust-lang.org/rustc/index.html\r\n\r\n(rustc book)\r\n\r\nsrc: https://www.rust-lang.org/learn\r\n\r\n## In-depth explanations of the errors you may see from the Rust compiler.\r\n\r\nhttps://doc.rust-lang.org/error-index.html\r\n\r\n(compiler error index)\r\n\r\nsrc: https://doc.rust-lang.org/error-index.html\r\n\r\n# Rust community\r\n\r\nhttps://users.rust-lang.org/\r\n\r\nhttps://t.me/rust_community\r\n\r\n## Using rustmon\r\n\r\n`rustmon fileName.rs` or `rmon fileName.rs`\r\n\r\nFor e.g., let say you have a rust program `myprogram.rs` then you'd need to run `rustmon myprogram.rs` from the cli to run it in monitor mode.\r\n\r\n## Install rust\r\n\r\n```\r\ncurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh\r\n```\r\n\r\nsrc: https://www.rust-lang.org/tools/install\r\n\r\n## System programming\r\n\r\nhttps://en.wikipedia.org/wiki/Systems_programming\r\n\r\n## Formatter with vscode on code save\r\n\r\nxxx note xxxx\r\n\r\n### Simply use rust-analyzer and uninstall origianl rust vscode extension\r\n\r\nIf you have some async code in your project then you must have a `rustfmt.toml` file in the root of your **ANY NESTED** project (with content: `edition = \"2018\"`) to make rust formatter work for that project too otherwise the files won't format at all. UPDATED AT: 4 FEB, 2022.\r\n\r\nThats how tekipeps did, and it feels amazing as now i don't need to Cargo.toml in any opened folder in vscode so vsocode can format the files and `rustfmt.toml` works as expected!!\r\nYikes!\r\n\r\nYou can use\r\n\r\nLEARN: `rustfmt \u003cfileName\u003e` to format a file as well.\r\n\r\n```bash\r\nAlso info from rust-analyzer (from its extension):\r\nrust-analyzer\r\nProvides support for rust-analyzer: novel LSP server for the Rust programming language.\r\n\r\nNote the extension may cause conflicts with the official Rust extension. It is recommended to disable the Rust extension when using the rust-analyzer extension.\r\n\r\nNote the project is in alpha status: it is already useful in practice, but can't be considered stable.\r\n```\r\n\r\nSimply install [this extension](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust) and make sure that you have a cargo.toml file in the root of folder that you\r\nhave opened in vscode to make format on save works!\r\n\r\nNOT WORKING: \u003e\u003e\u003e Search for setting in vscode like: `rust rustfmt_path` and add path for your own global `rustfmt.toml` file. \\*You must have official rust extension installed to\r\nsee this setting.\r\n\r\nGlobal rustfmt.toml file location =\u003e PR: https://github.com/rust-lang/rustfmt/pull/3280 .\r\n\r\nAn ideal Cargo.toml file should be place in the root of folder opened in vscode i.e.,\r\n\r\n(Bcoz Vscode's rust extension using command `cargo fmt --all` to format the code and throws error if the Cargo.toml file isn't formatted properly.)\r\n\r\n```toml\r\n[package]\r\nname = \"a-small-rust-app\"\r\nversion = \"0.1.0\"\r\n\r\n[[bin]]\r\nname = \"general\"\r\npath = \"01_println.rs\"\r\n```\r\n\r\n## Updating rustc via rustup\r\n\r\n```bash\r\nrustup update\r\n```\r\n\r\n## more..\r\n\r\n### Tips:\r\n\r\n0. Tutorial [here](https://www.youtube.com/playlist?list=PLVvjrrRCBy2JSHf9tGxGKJ-bYAN_uDCUL).\r\n\r\n0.5. Was [here](https://www.youtube.com/watch?v=wM6nmsNcyic\u0026list=PLVvjrrRCBy2JSHf9tGxGKJ-bYAN_uDCUL\u0026index=39).\r\n\r\n1. Whenever you see something like `car::bike::scooter` means that `car` is a module and `bike` is nested module inside `car` and `scooter` is a module nested inside `bike`.\r\n\r\n2. strings: [amazing article](https://chercher.tech/rust/string-in-rust), or read from [amazing docs :LOL:](https://doc.rust-lang.org/rust-by-example/std/str.html).\r\n\r\n3. [Gitignore](https://github.com/github/gitignore/blob/master/Rust.gitignore).\r\n\r\n4. [Primitives](https://doc.rust-lang.org/rust-by-example/primitives.html).\r\n\r\n5. Reqwest\r\n\r\n   - [Reqwest - some working examples @crates.io page, yikes!](https://crates.io/crates/reqwest),\r\n   - [Reqwest docs](https://docs.rs/reqwest/0.11.0/reqwest/)\r\n\r\n6. Rustler thing(popular idk why..): 1. [@docs](https://docs.rs/crate/rustler/0.21.1), 2. [@github](https://github.com/rusterlium/rustler),\r\n   [@crates.io site](https://crates.io/crates/rustler).\r\n\r\n### MISSION: → → → → → → finish till vid-42.\r\n\r\n[\\*\\*Continue the vid-35 tutorial/and making this gist. @ here](https://youtu.be/B7koBE7VDGo)\r\n\r\n### todo: Findout most popular web server from crate.io for rust??\r\n\r\n[Follow a different course, and it feel good too](https://www.youtube.com/playlist?list=PLkO5ggdQuRaaeFke7nWS4ajhFVZ1biE7_)\r\n\r\n## Rust important links:\r\n\r\n- https://rust-lang-nursery.github.io/rust-cookbook/web/clients.html\r\n\r\n- https://crates.io/ - The Rust community’s crate registry\r\n\r\n- [https://www.rust-lang.org/](https://www.rust-lang.org/)\r\n\r\n- [https://www.rust-lang.org/learn](https://www.rust-lang.org/learn) - Book, Course and Example.(Seems good) And other references to learn quickly.\r\n\r\n- [https://www.rust-lang.org/learn/get-started](https://www.rust-lang.org/learn/get-started) - A small rust app(demonstration, yikes).\r\n\r\n- [Rust Plaground](https://play.rust-lang.org/)\r\n\r\n- [Usage of semicolons in rust](https://stackoverflow.com/a/26665514/13994126 \"Usage of semicolons in rust\")\r\n\r\n- [Official Conferences and playlists](https://www.youtube.com/channel/UCaYhcUwRBNscFNUKTjgPFiA)\r\n\r\n- Variable Shadoing: When we declare a binding in nested scope which is already declared in its parent scope. [@ wikipedia](https://en.wikipedia.org/wiki/Variable_shadowing)\r\n\r\n- [Multipthreaded server @ rust docs](https://doc.rust-lang.org/book/ch20-02-multithreaded.html)\r\n\r\n### Creating rust projects with cargo ([source](https://youtu.be/_RfxLg6K9oE))\r\n\r\n```bash\r\n$ cargo new hello-world-cargo --bin  # --bin tells the cargo to create a application project, not a library project.\r\n$ cd hello-world-cargo\r\n$ cargo run # This will output `Hello world!`. Congrats!!\r\n$ `cargorun` or `cw` will run cargo in dev mode just like nodemon does, yikes!\r\n```\r\n\r\nThe cargo application has entry point from `src/main.rs` file. **All rust programs begin with a `main` function.**\r\n\r\nFirstly, anytime you make changes to `src/main.rs`, you need to run `cargo run` from cli. Secondly, `cargo.toml` file is similar to package.json file, as you'll find project info\r\nand dependencies there. Thirdly, you'll get a git initiated project with pre-made `.gitignore` file.\r\n\r\n### Compile and run with **rustc**\r\n\r\n```rust\r\n// File, main.rs\r\nfn main(){\r\n  println!(\"Hello world!\")\r\n}\r\n```\r\n\r\nCompile rust program via `rustc main.rs `and run it via `./main` (from bash) or `main.exe` (from command prompt).\r\n\r\n### [cargo init](https://doc.rust-lang.org/cargo/commands/cargo-init.html)\r\n\r\n```\r\n--bin\r\nCreate a package with a binary target (src/main.rs). This is the default behavior.\r\n--lib\r\nCreate a package with a library target (src/lib.rs).\r\n```\r\n\r\n## Snippets from the official rust vscode extension\r\n\r\nsrc: https://github.com/rust-lang/vscode-rust#snippets\r\n\r\n```txt\r\n## Snippets\r\nSnippets are code templates which expand into common boilerplate. IntelliSense includes snippet names as options when you type; select one by pressing enter. You can move to the next snippet 'hole' in the template by pressing tab. We provide the following snippets:\r\n\r\nfor - a for loop\r\nmacro_rules - declare a macro\r\nif let - an if let statement for executing code only when a pattern matches\r\nspawn - spawn a thread\r\nextern crate - insert an extern crate statement\r\nThis extension is deliberately conservative about snippets and doesn't include too many. If you want more, check out Trusty Rusty Snippets.\r\n```\r\n\r\nSo, fun.. below extension provides many rust snippets and officially recommended from rust vscode extension for snippets.\r\n\r\nhttps://marketplace.visualstudio.com/items?itemName=polypus74.trusty-rusty-snippets\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsahilrajput03%2Flearning_rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsahilrajput03%2Flearning_rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsahilrajput03%2Flearning_rust/lists"}