{"id":24468695,"url":"https://github.com/brianobot/learning_rust","last_synced_at":"2025-07-01T09:39:12.206Z","repository":{"id":239210677,"uuid":"797654822","full_name":"brianobot/learning_rust","owner":"brianobot","description":"Document My Journey  while learning the Rust Programming Language","archived":false,"fork":false,"pushed_at":"2025-05-07T06:25:34.000Z","size":953,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-07T07:35:09.372Z","etag":null,"topics":["code-snippets","learning","learning-by-doing","rust","rust-lang","rust-programming","rust-programming-language"],"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/brianobot.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,"zenodo":null}},"created_at":"2024-05-08T09:02:32.000Z","updated_at":"2025-04-10T10:54:39.000Z","dependencies_parsed_at":"2024-05-18T08:31:42.101Z","dependency_job_id":"415030e4-b225-4459-b3e0-c8aec33a78bd","html_url":"https://github.com/brianobot/learning_rust","commit_stats":null,"previous_names":["brianobot/learning_rust"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/brianobot/learning_rust","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianobot%2Flearning_rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianobot%2Flearning_rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianobot%2Flearning_rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianobot%2Flearning_rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brianobot","download_url":"https://codeload.github.com/brianobot/learning_rust/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brianobot%2Flearning_rust/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262937958,"owners_count":23387702,"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":["code-snippets","learning","learning-by-doing","rust","rust-lang","rust-programming","rust-programming-language"],"created_at":"2025-01-21T07:12:54.981Z","updated_at":"2025-07-01T09:39:12.172Z","avatar_url":"https://github.com/brianobot.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🦀 Rust Learning Repository\n\nWelcome to my Rust Learning Repository! This repository contains code snippets and exercises that I used while following the official Rust manual. It's a comprehensive resource for anyone looking to learn Rust, from beginners to more advanced users.\n\n## Introduction\n\nRust is a systems programming language that aims to provide memory safety without sacrificing performance. Known for its strong emphasis on safety, speed, and concurrency, Rust is a great language for both beginners and experienced programmers. This repository documents my journey of learning Rust through the official manual, and I hope it serves as a helpful guide for you as well.\n\n### Notice \n```rust\ncargo new package_name\n```\n\nis equivalent to\n\n```rust\ncargo new package_name --bin\n```\n\nSince both generate a binary project\n\nIn the Cargo.toml file we can specify multiple [[bin]] table to show the different binaries we want \nto generate from the project\n\n## TL;DR Survival Guide \n1. Everyone value has an owner in rust\n  ```rust\n  let s1 = String::from(\"Brian Obot\"); // here s1 one is the owner of the String\n  ```\n2. there can only be one owner at a time\n  ```rust\n  let s1 = String::from(\"Brian Obot\");\n  let s2 = s1; // here the value has been moved to a new owner s2 and s1 simply becomes inaccessible since \n  // it has not value now, it is snapped out of reality by Rust Thanos (s1 is invalidated )\n  ```\n  Moving values also applies to function, since passing values to function call is actually the same as moving the \n  value into the variable in the function call signature\n\n  ```rust\n  let s2 = String::from(\"Brian Obot\");\n\n  fn print_string(s: String) {\n    println!(\"{}\", s);\n  }\n\n  print_string(s2); // here the value from the string is moved inot s in the function and s2 is invalidated \n  ```\n3. When the owner goes out of scope, the value would be dropped\n   ```rust\n    // external scope \n    // scope refers to the region of code between curly brackets\n   {\n        // inside the curly braces it's own little scope\n        let secret = vec![1, 2, 3, 4].to_bytes();\n   } // aat this point, secret is going out of scope and it would be invalidated\n   ```\n\nwe can use the process of creating reference to values without taking ownership of the value\nthis can be done with the \u0026\u003cvariable_name\u003e syntax, creating reference is called ```borrowing```\n\n### Borrowing Rules\n- At any given time you can only have one mutable reference or any number of imutable references\n- references must always be valid (the borrow checker uses lifetimes to ensure references are always valid)\n  - this ensures that the references lifetime does not outlive the lifetime of the value\n\n\n### Rust Design Patterns\n\n1. Builder's Pattern: The power here is that we can construct objects in an infinite amount of ways\n   without much restriction. This is extremely useful when constructing complex objects that require step by step construction\n   ```rust\n   #[derive(Debug, Clone)]\n    struct BurgerBuilder {\n        components: Vec\u003cBurgerComponent\u003e\n      }\n\n      #[derive(Debug, Clone)]\n      enum BurgerComponent {\n      BottomBun,\n      Patty,\n      Tomatoe,\n      Cheese,\n      Lettuce,\n      TopBun,\n      }\n\n      impl BurgerBuilder {\n        fn new() -\u003e Self {\n          BurgerBuilder {\n            components: vec![BurgerComponent::BottomBun],\n          }\n        }\n\n        fn add_component(mut self, component: BurgerComponent) -\u003e BurgerBuilder {\n          self.components.push(component);\n          self\n        }\n      }\n\n\n    fn main() {\n        let burger = BurgerBuilder::new()\n        .add_component(BurgerComponent::Tomatoe)\n        .add_component(BurgerComponent::Lettuce)\n        .add_component(BurgerComponent::TopBun);\n\n        println!(\"Burger: {:?}\", burger);\n    }\n   ```\n\n2. Factory Pattern:\n   ```rust\n   trait Toy {\n        fn log(self);\n    }\n\n    struct Robot;\n    struct Car;\n\n    impl Toy for Robot {\n        fn log(self) {\n            println!(\"The is a toy robot\");\n        }\n    }\n\n\n    impl Toy for Car {\n        fn log(self) {\n            println!(\"The is a toy car\");\n        }\n    }\n\n    enum ToyType {\n        Robot,\n        Car,\n    }\n\n\n    struct Factory;\n\n    impl Factory {\n        fn build_toy(toy_type: ToyType) -\u003e Box\u003cdyn Toy\u003e {\n            match toy_type {\n                ToyType::Robot =\u003e Box::new(Robot),\n                ToyType::Car =\u003e Box::new(Car),\n            }\n        }\n    }\n\n\n    fn main() {\n        let toy_car = Factory::build_toy(ToyType::Car);\n        let toy_robot = Factory::build_toy(ToyType::Robot);\n    }   \n   ```\n\n3. RAII (Resource Acquisition is Initialization)\n  ```rust\n  use std::sync::{Arc, Mutex};\n\n    fn main() {\n        let chest = Mutex::\u003cu32\u003e::new(0);\n\n        {\n            let key = chest.lock();\n            let mut data  = key.unwrap();\n            *data += 1;\n        }\n \n        println!(\"{chest:?}\");\n    }\n  ```\n\n4. Type\n   ```rust\n    struct File\u003cState\u003e {\n        state: State,\n    }\n\n    struct Open;\n    struct Closed;\n\n\n    impl File\u003cClosed\u003e {\n        fn open(self) -\u003e File\u003cOpen\u003e {\n            println!(\"Opening the file\");\n            File { state: Open }\n        }\n    }\n\n    impl File\u003cOpen\u003e {\n        fn read(\u0026self) {\n            println!(\"Reading the file\");\n        }\n\n        fn write(\u0026self) {\n            println!(\"writing to the file\");\n        }\n\n        fn close(self) -\u003e File\u003cClosed\u003e {\n            println!(\"Closing the file\");\n            File { state: Closed }\n        }\n    }\n\n\n    fn main() {\n        let closed_file = File { state: Closed };\n        let open_file = closed_file.open();\n    }\n   ```\n\n## Contributing\n\nI welcome contributions from anyone! If you have any improvements or additional exercises you'd like to share, please feel free to fork this repository and submit a pull request.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianobot%2Flearning_rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrianobot%2Flearning_rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrianobot%2Flearning_rust/lists"}