{"id":25469857,"url":"https://github.com/thinkphp/computer-science-in-rust","last_synced_at":"2025-10-11T05:33:27.722Z","repository":{"id":66903114,"uuid":"159227855","full_name":"thinkphp/computer-science-in-rust","owner":"thinkphp","description":"Computer Science in Rust","archived":false,"fork":false,"pushed_at":"2025-02-24T15:13:00.000Z","size":1929,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T09:11:53.670Z","etag":null,"topics":["algorithm","apps","competitive-programming-contests","datastructures","machine-learning-algorithms","mathematics","rust"],"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/thinkphp.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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-11-26T20:23:31.000Z","updated_at":"2025-02-24T15:13:10.000Z","dependencies_parsed_at":"2024-12-30T10:26:51.026Z","dependency_job_id":"5da7d43a-e133-405c-9639-b39cd4037b63","html_url":"https://github.com/thinkphp/computer-science-in-rust","commit_stats":null,"previous_names":["thinkphp/computer-science-in-rust"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thinkphp/computer-science-in-rust","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thinkphp","download_url":"https://codeload.github.com/thinkphp/computer-science-in-rust/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thinkphp%2Fcomputer-science-in-rust/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279006323,"owners_count":26084085,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"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":["algorithm","apps","competitive-programming-contests","datastructures","machine-learning-algorithms","mathematics","rust"],"created_at":"2025-02-18T08:31:03.774Z","updated_at":"2025-10-11T05:33:27.705Z","avatar_url":"https://github.com/thinkphp.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Computer Science in Rust\n\n## Course Overview\nThis course introduces the Rust programming language, focusing on its unique features and common programming paradigms. By the end of this course, students will have a solid foundation in Rust programming and be able to write safe, concurrent, and efficient code.\n\n## Lesson 1: Rust Basics and Variables\n\n### 1.1 Hello, World!\nLet's start with the traditional \"Hello, World!\" program:\n\n```rust\nfn main() {\n    println!(\"Hello, Stanford!\");\n}\n```\n\nKey points:\n- `fn main()` is the entry point of every Rust program.\n- `println!` is a macro (note the `!`) for printing to the console.\n\n### 1.2 Variables and Mutability\nRust variables are immutable by default:\n\n```rust\nlet x = 5; // immutable\nlet mut y = 10; // mutable\n\ny = 15; // OK\nx = 6; // Error: cannot assign twice to immutable variable\n```\n\n### 1.3 Data Types\nRust is statically typed, but can infer types:\n\n```rust\nlet integer: i32 = 42;\nlet float = 3.14; // f64 by default\nlet boolean = true;\nlet character = 'A';\n```\n\n## Lesson 2: Control Flow\n\n### 2.1 If Expressions\n```rust\nlet number = 7;\n\nif number \u003c 5 {\n    println!(\"Number is less than 5\");\n} else if number \u003e 5 {\n    println!(\"Number is greater than 5\");\n} else {\n    println!(\"Number is 5\");\n}\n```\n\n### 2.2 Loops\nRust provides several looping constructs:\n\n```rust\n// loop\nlet mut counter = 0;\nloop {\n    println!(\"Count: {}\", counter);\n    counter += 1;\n    if counter == 5 {\n        break;\n    }\n}\n\n// while\nlet mut number = 3;\nwhile number != 0 {\n    println!(\"{}!\", number);\n    number -= 1;\n}\n\n// for\nfor i in 1..=5 {\n    println!(\"{}!\", i);\n}\n```\n\n## Lesson 3: Ownership and Borrowing\n\n### 3.1 Ownership Rules\n1. Each value has an owner.\n2. There can only be one owner at a time.\n3. When the owner goes out of scope, the value is dropped.\n\n```rust\n{\n    let s = String::from(\"hello\"); // s is valid from this point forward\n    // do stuff with s\n}  // s is no longer valid here\n```\n\n### 3.2 Borrowing\nRust allows borrowing references to values:\n\n```rust\nfn main() {\n    let s1 = String::from(\"hello\");\n    let len = calculate_length(\u0026s1);\n    println!(\"The length of '{}' is {}.\", s1, len);\n}\n\nfn calculate_length(s: \u0026String) -\u003e usize {\n    s.len()\n}\n```\n\n### 3.3 Mutable References\n```rust\nfn main() {\n    let mut s = String::from(\"hello\");\n    change(\u0026mut s);\n}\n\nfn change(some_string: \u0026mut String) {\n    some_string.push_str(\", world\");\n}\n```\n\n## Lesson 4: Structs and Enums\n\n### 4.1 Structs\n```rust\nstruct Rectangle {\n    width: u32,\n    height: u32,\n}\n\nimpl Rectangle {\n    fn area(\u0026self) -\u003e u32 {\n        self.width * self.height\n    }\n}\n\nfn main() {\n    let rect = Rectangle { width: 30, height: 50 };\n    println!(\"Area: {}\", rect.area());\n}\n```\n\n### 4.2 Enums and Pattern Matching\n```rust\nenum Coin {\n    Penny,\n    Nickel,\n    Dime,\n    Quarter,\n}\n\nfn value_in_cents(coin: Coin) -\u003e u8 {\n    match coin {\n        Coin::Penny =\u003e 1,\n        Coin::Nickel =\u003e 5,\n        Coin::Dime =\u003e 10,\n        Coin::Quarter =\u003e 25,\n    }\n}\n```\n\n## Lesson 5: Error Handling\n\n### 5.1 Panic!\n```rust\nfn main() {\n    panic!(\"crash and burn\");\n}\n```\n\n### 5.2 Result Enum\n```rust\nuse std::fs::File;\n\nfn main() {\n    let f = File::open(\"hello.txt\");\n\n    let f = match f {\n        Ok(file) =\u003e file,\n        Err(error) =\u003e panic!(\"Problem opening the file: {:?}\", error),\n    };\n}\n```\n\n## Lesson 6: Generics and Traits\n\n### 6.1 Generics\n```rust\nfn largest\u003cT: PartialOrd\u003e(list: \u0026[T]) -\u003e \u0026T {\n    let mut largest = \u0026list[0];\n\n    for item in list {\n        if item \u003e largest {\n            largest = item;\n        }\n    }\n\n    largest\n}\n```\n\n### 6.2 Traits\n```rust\ntrait Summary {\n    fn summarize(\u0026self) -\u003e String;\n}\n\nstruct NewsArticle {\n    headline: String,\n    location: String,\n    author: String,\n    content: String,\n}\n\nimpl Summary for NewsArticle {\n    fn summarize(\u0026self) -\u003e String {\n        format!(\"{}, by {} ({})\", self.headline, self.author, self.location)\n    }\n}\n```\n\n## Conclusion\nThis introduction to Rust covers the fundamental concepts of the language. Students are encouraged to practice these concepts and explore more advanced topics such as concurrency, smart pointers, and the module system.\n\n\n# Essential Rust.\n\nRust is a programming language that helps you write faster, more reliable software. High-level ergonomics and low-level control are often at odds with each other in programming language design; Rust stands to challenge that. Through balancing powerful technical capacity and a great developer experience, Rust gives you the option to control low-level details (such as memory usage) without all the hassle traditionally associated with such control.\n\n```\nextern crate rand;\n\nuse std::io;\nuse std::cmp::Ordering;\nuse rand::Rng;\n\nfn main() {\n    println!(\"Guess the number!\");\n\n    let secret_number = rand::thread_rng().gen_range(1, 101);\n\n    println!(\"The secret number is: {}\", secret_number);\n\n    println!(\"Please input your guess.\");\n\n    let mut guess = String::new();\n\n    io::stdin().read_line(\u0026mut guess)\n        .expect(\"Failed to read line\");\n\n    let guess: u32 = guess.trim().parse()\n        .expect(\"Please type a number!\");\n\n    println!(\"You guessed: {}\", guess);\n\n    match guess.cmp(\u0026secret_number) {\n        Ordering::Less =\u003e println!(\"Too small!\"),\n        Ordering::Greater =\u003e println!(\"Too big!\"),\n        Ordering::Equal =\u003e println!(\"You win!\"),\n    }\n}\n```  \n\n### Featuring\n    * pattern matching\n    * efficient C bindings\n    * type inference\n    * minimal runtime  \n\n\n```\n$ rustc hello.rs\n$ ./hello\nHello World!\n\nfn main() {\n    // Statements here are executed when the compiled binary is called.\n    \n    //println! is a macro that prints text to the console.\n    println!(\"Hello World!\");\n}\n```  \n\n## Scalar Types\n\n* Signed integers: i8, i16, i32, i64, i128 and isize (pointer size)\n* Unsigned integers: u8, u16, u32, u64, u128 and usize (pointer size)\n* Floating point: f32, f64\n* char Unicode scalar values like 'a', 'α' and '∞' (4 bytes each)\n* bool either true or false\n* The unit type (), whose only possible value is an empty tuple: ()\n* Despite the value of a unit type being a tuple, it is not considered a compound type because it does not contain multiple values.\n\n## While Control Flow\n\n```\nfn main() {\n    // A counter variable\n    let mut n = 1;\n\n    // Loop while `n` is less than 101\n    while n \u003c 101 {\n        if n % 15 == 0 {\n            println!(\"fizzbuzz\");\n        } else if n % 3 == 0 {\n            println!(\"fizz\");\n        } else if n % 5 == 0 {\n            println!(\"buzz\");\n        } else {\n            println!(\"{}\", n);\n        }\n\n        // Increment counter\n        n += 1;\n    }\n}\n```\n\n## if/else Control Flow\n```\nfn main() {\n    let n = 5;\n\n    if n \u003c 0 {\n        print!(\"{} is negative\", n);\n    } else if n \u003e 0 {\n        print!(\"{} is positive\", n);\n    } else {\n        print!(\"{} is zero\", n);\n    }\n\n    let big_n =\n        if n \u003c 10 \u0026\u0026 n \u003e -10 {\n            println!(\", and is a small number, increase ten-fold\");\n\n            // This expression returns an `i32`.\n            10 * n\n        } else {\n            println!(\", and is a big number, halve the number\");\n\n            // This expression must return an `i32` as well.\n            n / 2\n            // TODO ^ Try suppressing this expression with a semicolon.\n        };\n    //   ^ Don't forget to put a semicolon here! All `let` bindings need it.\n\n    println!(\"{} -\u003e {}\", n, big_n);\n}\n```\n\n ## Euclid's Algorithm https://ideone.com/Yv1jcj\n\n```rust\nuse std::io;\nuse std::str::FromStr;\nuse std::cmp::Ordering;\n\nfn get_number(prompt_input: \u0026str) -\u003e u32 {\n\n       println!(\"{}\", prompt_input);\n       \n       let mut input = String::new();\n       \n       io::stdin().read_line(\u0026mut input).expect(\"no input!\");     \n       \n       u32::from_str(input.trim()).unwrap()      \n}\n\nfn main() {\n     \n       let a = get_number(\"Enter first number a = \");\n       \n       let b = get_number(\"Enter first number b = \");\n\n       println!(\"The Greatest Common Divisor of {} and {} is {}\", a, b, euclid( a, b) );       \n}\n\nfn euclid(a: u32, b: u32) -\u003e u32 {\n \n   assert!(a \u003e 0 \u0026\u0026 b \u003e 0);\n   \n   match a.cmp(\u0026b) {\n   \n      Ordering::Equal =\u003e b,\n      \n      Ordering::Less =\u003e euclid(a,b-a),\n      \n      Ordering::Greater =\u003e euclid(a-b,b)\n   }   \n}\n```\n### Practice\nHundreds of companies, large and small, use Rust in production for a variety of tasks, including command line tools, web services, DevOps tooling, embedded devices, audio and video analysis and transcoding, cryptocurrencies, bioinformatics, search engines, Internet of Things applications, machine learning, and even major parts of the Firefox web browser.\n\n## References\n* https://doc.rust-lang.org/stable/book/\n* https://doc.rust-lang.org/stable/rust-by-example\n* https://cs.lmu.edu/~ray/notes/introrust/\n* https://stevedonovan.github.io/rust-gentle-intro/\n* https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/#the-rust-bookshelf\n* https://www.coursera.org/specializations/programming-with-rust\n\n## Playground\n- https://play.rust-lang.org/\n\n## Books:\n\n- (...)\n\n\u003cdetails\u003e\u003csummary\u003eInterview Questions\u003c/summary\u003e    \n\n-  https://github.com/imhq/rust-interview-handbook    \n-  https://www.turing.com/interview-questions/rust\n    \n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkphp%2Fcomputer-science-in-rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthinkphp%2Fcomputer-science-in-rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthinkphp%2Fcomputer-science-in-rust/lists"}