├── .github ├── pull_request_template.md └── workflows │ └── deploy.yml ├── .gitignore ├── CONTRIBUTING.md ├── FAQs.md ├── LICENSE ├── README.md ├── book.toml └── src ├── FAQs.md ├── RESOURCES.md ├── SUMMARY.md ├── about.md ├── chapter_1-basics └── questions.md ├── chapter_10-input&output └── questions.md ├── chapter_11-macros └── questions.md ├── chapter_12-concurrency └── questions.md ├── chapter_2-expressions └── questions.md ├── chapter_3-structs └── questions.md ├── chapter_4-enum&patterns └── questions.md ├── chapter_5-traits&generics └── questions.md ├── chapter_6-operatoroverloading&utility └── questions.md ├── chapter_7-closures&iterators └── questions.md ├── chapter_8-empty └── questions.md ├── chapter_9-collections └── questions.md └── chapter_miscellaneous └── questions.md /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | - [ ] I have read [CONTRIBUTING.md](https://github.com/sn99/rust-practise-questions/blob/master/CONTRIBUTING.md) 2 | - [ ] Link to solution is : PLAYGROUND_LINK 3 | - [ ] This is not a ReWrite of an included question concept 4 | 5 | Thank you for Contributing -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Deploy 2 | on: 3 | push: 4 | branches: 5 | - master 6 | 7 | jobs: 8 | deploy: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | with: 13 | fetch-depth: 0 14 | - name: Install mdbook 15 | run: | 16 | mkdir mdbook 17 | curl -sSL https://github.com/rust-lang/mdBook/releases/download/v0.4.14/mdbook-v0.4.14-x86_64-unknown-linux-gnu.tar.gz | tar -xz --directory=./mdbook 18 | echo `pwd`/mdbook >> $GITHUB_PATH 19 | - name: Deploy GitHub Pages 20 | run: | 21 | # This assumes your book is in the root of your repository. 22 | # Just add a `cd` here if you need to change to another directory. 23 | mdbook build 24 | git worktree add gh-pages 25 | git config user.name "Deploy from CI" 26 | git config user.email "" 27 | cd gh-pages 28 | # Delete the ref to avoid keeping history. 29 | git update-ref -d refs/heads/gh-pages 30 | rm -rf * 31 | mv ../book/* . 32 | git add . 33 | git commit -m "Deploy $GITHUB_SHA to gh-pages" 34 | git push --force --set-upstream origin gh-pages -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | book 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute 2 | 3 | The book has been divided into sections for easy addition and removal of questions 4 | 5 | I have left `chapter 8` empty (changing names is boring) 6 | 7 | You might wanna read [mdbook guide](https://rust-lang-nursery.github.io/mdBook/) before contributing 8 | 9 | All the questions are in the following format `src/chapterX/questions.md` 10 | 11 | All your contributions will be considered under [MIT LICENSE](LICENSE) 12 | 13 | Finally execute `mdbook build` after adding questions (provide one solution 14 | in [playground](https://play.rust-lang.org/)) and try not 15 | include questions that convey the same concept -------------------------------------------------------------------------------- /FAQs.md: -------------------------------------------------------------------------------- 1 | #### Where can I find solutions to the problems in this book ? 2 | 3 | I have decided not to provide solutions to any problem in this book, you will have to use the long way(irc channels, 4 | stackoverflow, etc). 5 | 6 | The reason for this is so that you have to study the whole concept rather than a part of it because I feel a lot of new 7 | learners simply go and check the solution after being stuck for the first time. 8 | 9 | Though help will be provided in some questions as a form of `hint` 10 | 11 | #### How can I be sure whether a question has a solution or not ? 12 | 13 | Each and every question that appears in the book has been solved in at least in 1 way before making it's way in the book 14 | 15 | #### Where can I find help ? 16 | 17 | Some of the sources that are the most helpful are : 18 | 19 | * [Discord Channel for beginners](https://discord.gg/rust-lang) 20 | * [Stackoverflow](https://stackoverflow.com/questions/tagged/rust) 21 | * [Reddit community](https://www.reddit.com/r/rust/) 22 | 23 | #### I am unable to solve so-and-so question, why ? 24 | 25 | Mostly because you may be new to the language or to programming world itself, try again and keep on learning, you will 26 | be able to eventually solve them with ease -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Siddharth Naithani 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rust-practise-questions 2 | 3 | [![Deploy](https://github.com/rust-unofficial/rust-practise-questions/actions/workflows/deploy.yml/badge.svg)](https://github.com/rust-unofficial/rust-practise-questions/actions/workflows/deploy.yml) 4 | [![pages-build-deployment](https://github.com/rust-unofficial/rust-practise-questions/actions/workflows/pages/pages-build-deployment/badge.svg)](https://github.com/rust-unofficial/rust-practise-questions/actions/workflows/pages/pages-build-deployment) 5 | 6 | [![Visit Book](https://img.shields.io/badge/Book-Rendered-brightgreen.svg?style=for-the-badge)](https://rust-unofficial.github.io/rust-practise-questions/) 7 | 8 | A set of questions for Rust lang 9 | 10 | ## About 11 | 12 | The book is written so that a newbie [Rustaceans](https://www.rustaceans.org/) can easily dive in the language and get 13 | productive as fast as possible 14 | 15 | ### What this book is not 16 | 17 | This book is not an attempt to teach you [Rust](https://www.rust-lang.org/), it simply provides question for practise, 18 | and **it is not a tutorial** 19 | 20 | ## Contributing 21 | 22 | Read [CONTRIBUTING](CONTRIBUTING.md) for contributing to the book 23 | 24 | See also the list of [contributors](https://github.com/sn99/rust-practise-questions/graphs/contributors) who 25 | participated in this project. 26 | 27 | ## FAQ 28 | 29 | #### Where can I find solutions to the problems in this book ? 30 | 31 | I have decided not to provide solutions to any problem in this book, you will have to use the long way(irc channels, 32 | stackoverflow, etc). 33 | 34 | The reason for this is so that you have to study the whole concept rather than a part of it because I feel a lot of new 35 | learners simply go and check the solution after being stuck for the first time. 36 | 37 | Though help will be provided in some questions as a form of `hint` 38 | 39 | #### How can I be sure whether a question has a solution or not ? 40 | 41 | Each and every question that appears in the book has been solved in at least in 1 way before making it's way in the book 42 | 43 | #### Where can I find help ? 44 | 45 | Some of the sources that are the most helpful are : 46 | 47 | * [Discord Channel for beginners](https://discord.gg/rust-lang) 48 | * [Stackoverflow](https://stackoverflow.com/questions/tagged/rust) 49 | * [Reddit community](https://www.reddit.com/r/rust/) 50 | 51 | #### I am unable to solve so-and-so question, why ? 52 | 53 | Mostly because you may be new to the language or to programming world itself, try again and keep on learning, you will 54 | be able to eventually solve them with ease 55 | 56 | ## License 57 | 58 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details -------------------------------------------------------------------------------- /book.toml: -------------------------------------------------------------------------------- 1 | [book] 2 | authors = [" Siddharth Naithani aka sn99"] 3 | src = "src" 4 | title = "Jang" 5 | description = "Rust Practise Questions" 6 | -------------------------------------------------------------------------------- /src/FAQs.md: -------------------------------------------------------------------------------- 1 | # FAQs 2 | 3 | #### Where can I find solutions to the problems in this book ? 4 | 5 | I have decided not to provide solutions to any problem in this book, you will have to use the long way(irc channels, 6 | stackoverflow, etc). 7 | 8 | The reason for this is so that you have to study the whole concept rather than a part of it because I feel a lot of new 9 | learners simply go and check the solution after being stuck for the first time. 10 | 11 | Though help will be provided in some questions as a form of `hint` 12 | 13 | #### How can I be sure whether a question has a solution or not ? 14 | 15 | Each and every question that appears in the book has been solved in at least in 1 way before making it's way in the book 16 | 17 | #### Where can I find help ? 18 | 19 | Some of the sources that are the most helpful are : 20 | 21 | * [Discord Channel for beginners](https://discord.gg/rust-lang) 22 | * [Stackoverflow](https://stackoverflow.com/questions/tagged/rust) 23 | * [Reddit community](https://www.reddit.com/r/rust/) 24 | 25 | #### I am unable to solve so-and-so question, why ? 26 | 27 | Mostly because you may be new to the language or to programming world itself, try again and keep on learning, you will 28 | be able to eventually solve them with ease -------------------------------------------------------------------------------- /src/RESOURCES.md: -------------------------------------------------------------------------------- 1 | # RESOURCES 2 | 3 | Some of the sources that might help you in learning [Rust](https://www.rust-lang.org/) 4 | 5 | * [The rust book (main)](https://doc.rust-lang.org/book/index.html) - It is the main book and should be your companion 6 | till the end 7 | * [A gentle introduction to rust](https://stevedonovan.github.io/rust-gentle-intro/readme.html) 8 | * [Learning Rust](https://learning-rust.github.io) 9 | * [The official site for Documentation](https://www.rust-lang.org/en-US/documentation.html) 10 | * [rust-learning](https://github.com/ctjhoa/rust-learning) - a community maintained learning resources 11 | * [rust-by-example](https://doc.rust-lang.org/rust-by-example) - A practical book to teach you rust 12 | 13 | The above resources are more than enough to help you learn your way into the world of Rust -------------------------------------------------------------------------------- /src/SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | [About](about.md) 4 | [FAQs](FAQs.md) 5 | [Learning Resources](RESOURCES.md) 6 | 7 | - [Chapter 1 - Basics](chapter_1-basics/questions.md) 8 | - [Chapter 2 - Expressions](chapter_2-expressions/questions.md) 9 | - [Chapter 3 - Structs](chapter_3-structs/questions.md) 10 | - [Chapter 4 - Enum & Patterns](chapter_4-enum&patterns/questions.md) 11 | - [Chapter 5 - Traits & Generics](chapter_5-traits&generics/questions.md) 12 | - [Chapter 6 - Operator Overloading & Utility Traits](chapter_6-operatoroverloading&utility/questions.md) 13 | - [Chapter 7 - Closures & Iterators](chapter_7-closures&iterators/questions.md) 14 | - [Chapter 8 - Collections](chapter_9-collections/questions.md) 15 | - [Chapter 9 - Input & Output](chapter_10-input&output/questions.md) 16 | - [Chapter 10 - Macros](chapter_11-macros/questions.md) 17 | - [Chapter 11 - Concurrency](chapter_12-concurrency/questions.md) 18 | - [Chapter - Miscellaneous](chapter_miscellaneous/questions.md) 19 | 20 | -------------------------------------------------------------------------------- /src/about.md: -------------------------------------------------------------------------------- 1 | The source code of the book is at [link](https://github.com/rust-unofficial/rust-practise-questions) 2 | 3 | # about 4 | 5 | The book is written so that a newbie [Rustaceans](https://www.rustaceans.org/) can easily dive in the language and get 6 | productive as fast as possible 7 | 8 | ## What this book is not 9 | 10 | This book is not an attempt to teach you [Rust](https://www.rust-lang.org/), it simply provides question for practise, 11 | and **it is not a tutorial** 12 | -------------------------------------------------------------------------------- /src/chapter_1-basics/questions.md: -------------------------------------------------------------------------------- 1 | # Chapter 1 - Basics 2 | 3 | * Write a program to print `Hello, World`. 4 | 5 | * Write a program to print a block F using hash (#), where the F has a height of six characters and width of five and 6 | four characters. 7 | 8 | ``` 9 | ###### 10 | # 11 | # 12 | ##### 13 | # 14 | # 15 | # 16 | ``` 17 | 18 | * Create a `tuple` to hold an `i32` and `f32` and then call the `f32` value. 19 | 20 | * Take input(name) from the user of type `String` and print `Hello, !`. 21 | 22 | * Initialize an array of sixteen `0` -------------------------------------------------------------------------------- /src/chapter_10-input&output/questions.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-unofficial/rust-practise-questions/9820493bcf7edd50a444d887e73b831aa0f305b0/src/chapter_10-input&output/questions.md -------------------------------------------------------------------------------- /src/chapter_11-macros/questions.md: -------------------------------------------------------------------------------- 1 | * Implement a macro named `addition` to add any amount of numbers. `Eg`: `addition!(5, 6, 57 ,56, 1)` 2 | should return `125` and `addition!(4, 9)` should return `11`. 3 | 4 | -------------------------------------------------------------------------------- /src/chapter_12-concurrency/questions.md: -------------------------------------------------------------------------------- 1 | * Spawn 2 threads, one that continuously says `Hello` and the other that say `World`. 2 | 3 | * Find out the number of physical and logical cores in your CPU using rust. 4 | `hint:` try using `num_cpus` crate. 5 | -------------------------------------------------------------------------------- /src/chapter_2-expressions/questions.md: -------------------------------------------------------------------------------- 1 | # Chapter 2 - Expressions 2 | 3 | * Run an infinite `loop` to keep on printing `Hello, World!`.\ 4 | `hint:` you might want to use `ctrl+c` or `ctrl+z` to exit the infinite loop. 5 | 6 | * Take a `integer` input from the user and print table for that integer using a loop. 7 | 8 | * Print the following pattern using loops. 9 | ``` 10 | * 11 | *** 12 | ***** 13 | ******* 14 | ********* 15 | ``` 16 | 17 | * Check whether the input number is odd or even and print `odd` or `even` respectively. 18 | 19 | * Find and print factorial of a program using recursion. 20 | 21 | * Using a match statement to print `one` for input `1`, `two` for `2` and so on, and `NaN` on default. 22 | 23 | * Create a diverging function for addition of 2 numbers. 24 | 25 | * Create a program to check for leap year.\ 26 | `note:` 1900 is not a leap year. 27 | 28 | * Write a function to swap 2 numbers. 29 | 30 | * Write a program to find prime numbers upto a number `N` 31 | 32 | * WAP to sort a list of numbers. 33 | 34 | * WAP to iterate over 2 vectors at once.\ 35 | `hint:` try using `.zip` method. -------------------------------------------------------------------------------- /src/chapter_3-structs/questions.md: -------------------------------------------------------------------------------- 1 | # Chapter 3 - Structs 2 | 3 | * Write a program to store and print the roll no., name , age and marks of a student using structures. 4 | 5 | * Write a program to compare two dates entered by user. Make a structure named Date to store the elements day, month and 6 | year to store the dates. If the dates are equal, display "Dates are equal" otherwise display "Dates are not equal". 7 | 8 | * Write a program to add, subtract and multiply two complex numbers using structures to function. 9 | 10 | * Create a structure named Date having day, month and year as its elements. Store the current date in the structure. Now 11 | add 45 days to the current date and display the final date. 12 | 13 | * Replicate constructor function with name `new()` returning type `Self`. 14 | 15 | * Let us work on the menu of a library. Create a structure containing book information like accession number, name of 16 | author, book title and flag to know whether book is issued or not. 17 | Create a menu in which the following can be done.\ 18 | 1 - Display book information\ 19 | 2 - Add a new book\ 20 | 3 - Display all the books in the library of a particular author\ 21 | 4 - Display the number of books of a particular title\ 22 | 5 - Display the total number of books in the library\ 23 | 6 - Issue a book\ 24 | (If we issue a book, then its number gets decreased by 1 and if we add a book, its number gets increased by 1) 25 | 26 | * Create a generic struct for addition of numbers (they can be `integer` or `float`). 27 | 28 | * Create a struct with `Copy, Clone, Debug, PartialEq` traits\ 29 | `hint:` Use `#[derive]` 30 | 31 | * Create an immutable struct with a mutable member.\ 32 | `hint:` Use `Cell`, property known as interior mutability. 33 | 34 | * Try making the above program with `RefCell` so that the struct stores details about a bank(balance, customer count, 35 | location, etc) with only customer count being mutable. -------------------------------------------------------------------------------- /src/chapter_4-enum&patterns/questions.md: -------------------------------------------------------------------------------- 1 | # Chapter 4 - Enum & Patterns 2 | 3 | * Create an enum that contains HTTP 4xx Client Errors.\ 4 | `i.e.` `Not Found` Should have value `404` associated with it. 5 | 6 | * Write an enum to store information of whether a person is a child or adult based on his/her age. 7 | 8 | * Create a calculator with the help of an enum named `Operation` that as values such as `Add, Sub, Div, Multi`.\ 9 | `hint:` For input like `2+5*10` it should first evaluate `5*10` and then add 2 to it. 10 | 11 | * Use pattern matching to associate a enum of `Grade` type with a student based on his/her marks. 12 | 13 | * Create an enum named `Json` that can work with arbitrary JSON data. 14 | 15 | * Print what kind of input the user has given(numbers, letters, symbols) using pattern matching.\ 16 | `hint:` Try using range for it e.g. `0 ... 100` or `'a' ... 'k'` 17 | 18 | * Use pattern matching to find that whether a point lies on X-Axis, Y-Axis or on which quadrant. 19 | 20 | * Create a struct that holds info about a gun(gun type, recoil time, magazine size, extra) where `gun type` and 21 | `extra` are enums and `extra` contains silencer, scope, extended mags nad `None`.\ 22 | Based on user input change the value of extra (may cause change in recoil time and magazine size). 23 | 24 | * Create a Binary tree and have a method `add` on it to add elements to it and `min` and `max` to find the minimum and 25 | maximum element in it. 26 | 27 | * Create a `Regex` to extract dates from this string `It was on 2019-03-14, almost after a year from 2018-02-11` and 28 | store in a 29 | Struct with fields of `day`, `month` and `year`.\ 30 | `hint:` Use `Regex` crate 31 | -------------------------------------------------------------------------------- /src/chapter_5-traits&generics/questions.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 - Traits & Generics 2 | 3 | * Derive a debug trait to print info about your struct that contains `name`, `c1ass` and `roll`. 4 | 5 | * Create a generic function to get min of 2 values.\ 6 | `hint:` You might need to use `Ord` trait bound. 7 | 8 | * Implement custom `Drop` trait for a struct name `Student` that contains your `name`, `age` and `roll number`. It 9 | should return `Roll number has name with age and is a `. Being Junior or 10 | Senior depends 11 | on age (18 or above). 12 | 13 | * Implement a custom `Debug` message for the above Struct. 14 | 15 | * Implement custom `Iterator` trait for a struct named `GeometricSeries` that has 16 | 3 fields `first_number`, `current_number` and `ratio` of type `i32`. The iterator should return the 17 | next 11 numbers in geometric progression.\ 18 | `hint:` Use `.take(11)` to get the next 11 in `for` loop. 19 | 20 | * Implement the same as above for a `FibonacciSeries` struct. 21 | 22 | * Implement a generic function name `sum` with additional parameter of `index: usize` 23 | that can take either `GeometricSeries` or `FibonacciSeries` and returns the sum upto the given index.\ 24 | `hint:` use `T: Iterator` where `T` is generic 25 | 26 | * Write a generic function name `Multiply` that multiplies all `usize`, `isize` and `fsize` type values. 27 | 28 | * Make a generic function to return the greater of the 2 given values (integer and floats). 29 | 30 | * Implement a trait named `Hello` with a default function `say(&self)` that prints `Hello, !` 31 | , Implement this for `str` and `string` without providing any definition of `Hello` (simply `impl Hello for str {}`) 32 | call `say` on str `World`. 33 | -------------------------------------------------------------------------------- /src/chapter_6-operatoroverloading&utility/questions.md: -------------------------------------------------------------------------------- 1 | * Create a struct name `Set` that contains a `Vector` of chars and overload the `minus` 2 | operator so that when 2 structs subtract it removes the chars of 2nd from 1st one. 3 | 4 | * Create a struct named `ComplexNumber` that has 2 variables `re` & `im` and overload `minus` 5 | and `plus` operator to add and subtract complex number. 6 | 7 | * Overload the `!` operator to conjugate the complex number `!ComplexNumber` and `==` and `!=` for comparison. 8 | 9 | * Create a struct named `Class` that contains the class size, section and grade. Overload 10 | the `>`, `<`, `>=`, `<=`, `==` operators to compare class sizes of various Classes. 11 | 12 | * Rust does not allow addition of `integer` and `float`. Overload `+` so that this is possible. 13 | 14 | * Implement custom `Drop` for a struct `Hero` that contains a field `name` of type string. 15 | The `drop` should print "Oh no !!! Our hero {name} is defeated". Run the program with just 16 | declaring a variable of type `Hero`. 17 | 18 | * Create the struct named `World` that contains the previous named struct `Hero`, define `drop` 19 | for it so that it prints "The world ends here !!!". Observe the order in which `World` and `Hero` 20 | contained by it are dropped. 21 | 22 | * Create a struct named `Table` that has a generic field `legs_info` of type `T`. Create a function (not a method) 23 | named `show` that accepts function parameters `&Table` and displays `legs_info`. Create 2 variables one 24 | that contains `T` of type `String: "Work in progress..."` and `usize: 4`.\ 25 | `hint:` use `?Sized`. 26 | 27 | * Implement `From` trait for struct `ComplexNumber` given a tuple of type `(isize, isize)`. Form it using `from` 28 | and `into` respectively. 29 | 30 | * Create a function that accepts either `ComplexNumber` or `(isize, isize)` to return the `mod` of ComplexNumber. 31 | -------------------------------------------------------------------------------- /src/chapter_7-closures&iterators/questions.md: -------------------------------------------------------------------------------- 1 | * Create a closer `add_one` to add 1 to an integer. 2 | 3 | * Create an vec of numbers from `0` ... `100`, mutate the vec so that 4 | it does not contain any number divisible by `3`. Use `retain` method of Vec. 5 | 6 | * Create a function that accepts a closure -------------------------------------------------------------------------------- /src/chapter_8-empty/questions.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-unofficial/rust-practise-questions/9820493bcf7edd50a444d887e73b831aa0f305b0/src/chapter_8-empty/questions.md -------------------------------------------------------------------------------- /src/chapter_9-collections/questions.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-unofficial/rust-practise-questions/9820493bcf7edd50a444d887e73b831aa0f305b0/src/chapter_9-collections/questions.md -------------------------------------------------------------------------------- /src/chapter_miscellaneous/questions.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-unofficial/rust-practise-questions/9820493bcf7edd50a444d887e73b831aa0f305b0/src/chapter_miscellaneous/questions.md --------------------------------------------------------------------------------