Skip to content

Commit

Permalink
Merge pull request #1 from akhildevelops/solutions
Browse files Browse the repository at this point in the history
Solutions
  • Loading branch information
akhildevelops authored Nov 13, 2022
2 parents 97e3c41 + 312f10f commit aecb6c4
Show file tree
Hide file tree
Showing 95 changed files with 485 additions and 517 deletions.
244 changes: 121 additions & 123 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,176 +1,174 @@
# rustlings 🦀❤️
# Solutions to rustlings 🦀❤️

Greetings and welcome to `rustlings`. This project contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!
## Solutions to [Rustlings exercises](https://github.com/rust-lang/rustlings) for latest release v5.2.1

_...looking for the old, web-based version of Rustlings? Try [here](https://github.com/rust-lang/rustlings/tree/rustlings-1)_
### Topic wise solutions:

Alternatively, for a first-time Rust learner, there are several other resources:
### **intro**
- [intro1](exercises/intro/intro1.rs)
- [intro2](exercises/intro/intro2.rs)

- [The Book](https://doc.rust-lang.org/book/index.html) - The most comprehensive resource for learning Rust, but a bit theoretical sometimes. You will be using this along with Rustlings!
- [Rust By Example](https://doc.rust-lang.org/rust-by-example/index.html) - Learn Rust by solving little exercises! It's almost like `rustlings`, but online

## Getting Started
### **variables**
- [variables1](exercises/variables/variables1.rs)
- [variables2](exercises/variables/variables2.rs)
- [variables3](exercises/variables/variables3.rs)
- [variables4](exercises/variables/variables4.rs)
- [variables5](exercises/variables/variables5.rs)
- [variables6](exercises/variables/variables6.rs)

_Note: If you're on MacOS, make sure you've installed Xcode and its developer tools by typing `xcode-select --install`._
_Note: If you're on Linux, make sure you've installed gcc. Deb: `sudo apt install gcc`. Yum: `sudo yum -y install gcc`._

You will need to have Rust installed. You can get it by visiting https://rustup.rs. This'll also install Cargo, Rust's package/project manager.
### **functions**
- [functions1](exercises/functions/functions1.rs)
- [functions2](exercises/functions/functions2.rs)
- [functions3](exercises/functions/functions3.rs)
- [functions4](exercises/functions/functions4.rs)
- [functions5](exercises/functions/functions5.rs)

## MacOS/Linux

Just run:
### **if**
- [if1](exercises/if/if1.rs)
- [if2](exercises/if/if2.rs)

```bash
curl -L https://raw.githubusercontent.com/rust-lang/rustlings/main/install.sh | bash
# Or if you want it to be installed to a different path:
curl -L https://raw.githubusercontent.com/rust-lang/rustlings/main/install.sh | bash -s mypath/
```

This will install Rustlings and give you access to the `rustlings` command. Run it to get started!
### **quiz1**
- [quiz1](exercises/quiz1.rs)

### Nix
Basically: Clone the repository at the latest tag, finally run `nix develop` or `nix-shell`.

```bash
# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 5.2.1)
git clone -b 5.2.1 --depth 1 https://github.com/rust-lang/rustlings
cd rustlings
# if nix version > 2.3
nix develop
# if nix version <= 2.3
nix-shell
```
### **primitive_types**
- [primitive_types1](exercises/primitive_types/primitive_types1.rs)
- [primitive_types2](exercises/primitive_types/primitive_types2.rs)
- [primitive_types3](exercises/primitive_types/primitive_types3.rs)
- [primitive_types4](exercises/primitive_types/primitive_types4.rs)
- [primitive_types5](exercises/primitive_types/primitive_types5.rs)
- [primitive_types6](exercises/primitive_types/primitive_types6.rs)

## Windows

In PowerShell (Run as Administrator), set `ExecutionPolicy` to `RemoteSigned`:
### **vecs**
- [vecs1](exercises/vecs/vecs1.rs)
- [vecs2](exercises/vecs/vecs2.rs)

```ps1
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
```

Then, you can run:
### **move_semantics**
- [move_semantics1](exercises/move_semantics/move_semantics1.rs)
- [move_semantics2](exercises/move_semantics/move_semantics2.rs)
- [move_semantics3](exercises/move_semantics/move_semantics3.rs)
- [move_semantics4](exercises/move_semantics/move_semantics4.rs)
- [move_semantics5](exercises/move_semantics/move_semantics5.rs)
- [move_semantics6](exercises/move_semantics/move_semantics6.rs)

```ps1
Start-BitsTransfer -Source https://raw.githubusercontent.com/rust-lang/rustlings/main/install.ps1 -Destination $env:TMP/install_rustlings.ps1; Unblock-File $env:TMP/install_rustlings.ps1; Invoke-Expression $env:TMP/install_rustlings.ps1
```

To install Rustlings. Same as on MacOS/Linux, you will have access to the `rustlings` command after it.
### **structs**
- [structs1](exercises/structs/structs1.rs)
- [structs2](exercises/structs/structs2.rs)
- [structs3](exercises/structs/structs3.rs)

If you get a permission denied message, you might have to exclude the directory where you cloned Rustlings in your antivirus.

## Browser
### **enums**
- [enums1](exercises/enums/enums1.rs)
- [enums2](exercises/enums/enums2.rs)
- [enums3](exercises/enums/enums3.rs)

[Run on Repl.it](https://repl.it/github/rust-lang/rustlings)

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/rust-lang/rustlings)
### **strings**
- [strings1](exercises/strings/strings1.rs)
- [strings2](exercises/strings/strings2.rs)
- [strings3](exercises/strings/strings3.rs)
- [strings4](exercises/strings/strings4.rs)

## Manually

Basically: Clone the repository at the latest tag, run `cargo install --path .`.
### **modules**
- [modules1](exercises/modules/modules1.rs)
- [modules2](exercises/modules/modules2.rs)
- [modules3](exercises/modules/modules3.rs)

```bash
# find out the latest version at https://github.com/rust-lang/rustlings/releases/latest (on edit 5.2.1)
git clone -b 5.2.1 --depth 1 https://github.com/rust-lang/rustlings
cd rustlings
cargo install --force --path .
```

If there are installation errors, ensure that your toolchain is up to date. For the latest, run:
### **hashmaps**
- [hashmaps1](exercises/hashmaps/hashmaps1.rs)
- [hashmaps2](exercises/hashmaps/hashmaps2.rs)
- [hashmaps3](exercises/hashmaps/hashmaps3.rs)

```bash
rustup update
```

Then, same as above, run `rustlings` to get started.
### **quiz2**
- [quiz2](exercises/quiz2.rs)

## Doing exercises

The exercises are sorted by topic and can be found in the subdirectory `rustlings/exercises/<topic>`. For every topic there is an additional README file with some resources to get you started on the topic. We really recommend that you have a look at them before you start.
### **options**
- [options1](exercises/options/options1.rs)
- [options2](exercises/options/options2.rs)
- [options3](exercises/options/options3.rs)

The task is simple. Most exercises contain an error that keeps them from compiling, and it's up to you to fix it! Some exercises are also run as tests, but rustlings handles them all the same. To run the exercises in the recommended order, execute:

```bash
rustlings watch
```
### **error_handling**
- [errors1](exercises/error_handling/errors1.rs)
- [errors2](exercises/error_handling/errors2.rs)
- [errors3](exercises/error_handling/errors3.rs)
- [errors4](exercises/error_handling/errors4.rs)
- [errors5](exercises/error_handling/errors5.rs)
- [errors6](exercises/error_handling/errors6.rs)

This will try to verify the completion of every exercise in a predetermined order (what we think is best for newcomers). It will also rerun automatically every time you change a file in the `exercises/` directory. If you want to only run it once, you can use:

```bash
rustlings verify
```
### **generics**
- [generics1](exercises/generics/generics1.rs)
- [generics2](exercises/generics/generics2.rs)

This will do the same as watch, but it'll quit after running.

In case you want to go by your own order, or want to only verify a single exercise, you can run:
### **traits**
- [traits1](exercises/traits/traits1.rs)
- [traits2](exercises/traits/traits2.rs)
- [traits3](exercises/traits/traits3.rs)
- [traits4](exercises/traits/traits4.rs)
- [traits5](exercises/traits/traits5.rs)

```bash
rustlings run myExercise1
```

Or simply use the following command to run the next unsolved exercise in the course:
### **quiz3**
- [quiz3](exercises/quiz3.rs)

```bash
rustlings run next
```

In case you get stuck, you can run the following command to get a hint for your
exercise:
### **tests**
- [tests1](exercises/tests/tests1.rs)
- [tests2](exercises/tests/tests2.rs)
- [tests3](exercises/tests/tests3.rs)

```bash
rustlings hint myExercise1
```

You can also get the hint for the next unsolved exercise with the following command:
### **lifetimes**
- [lifetimes1](exercises/lifetimes/lifetimes1.rs)
- [lifetimes2](exercises/lifetimes/lifetimes2.rs)
- [lifetimes3](exercises/lifetimes/lifetimes3.rs)

```bash
rustlings hint next
```

To check your progress, you can run the following command:
### **standard_library_types**
- [iterators1](exercises/standard_library_types/iterators1.rs)
- [iterators2](exercises/standard_library_types/iterators2.rs)
- [iterators3](exercises/standard_library_types/iterators3.rs)
- [iterators4](exercises/standard_library_types/iterators4.rs)
- [iterators5](exercises/standard_library_types/iterators5.rs)
- [box1](exercises/standard_library_types/box1.rs)
- [arc1](exercises/standard_library_types/arc1.rs)

```bash
rustlings list
```

## Testing yourself
### **threads**
- [threads1](exercises/threads/threads1.rs)
- [threads2](exercises/threads/threads2.rs)
- [threads3](exercises/threads/threads3.rs)

After every couple of sections, there will be a quiz that'll test your knowledge on a bunch of sections at once. These quizzes are found in `exercises/quizN.rs`.

## Enabling `rust-analyzer`
### **macros**
- [macros1](exercises/macros/macros1.rs)
- [macros2](exercises/macros/macros2.rs)
- [macros3](exercises/macros/macros3.rs)
- [macros4](exercises/macros/macros4.rs)

Run the command `rustlings lsp` which will generate a `rust-project.json` at the root of the project, this allows [rust-analyzer](https://rust-analyzer.github.io/) to parse each exercise.

## Continuing On
### **clippy**
- [clippy1](exercises/clippy/clippy1.rs)
- [clippy2](exercises/clippy/clippy2.rs)
- [clippy3](exercises/clippy/clippy3.rs)

Once you've completed Rustlings, put your new knowledge to good use! Continue practicing your Rust skills by building your own projects, contributing to Rustlings, or finding other open-source projects to contribute to.

## Uninstalling Rustlings

If you want to remove Rustlings from your system, there are two steps. First, you'll need to remove the exercises folder that the install script created
for you:

```bash
rm -rf rustlings # or your custom folder name, if you chose and or renamed it
```

Second, since Rustlings got installed via `cargo install`, it's only reasonable to assume that you can also remove it using Cargo, and
exactly that is the case. Run `cargo uninstall` to remove the `rustlings` binary:

```bash
cargo uninstall rustlings
```

Now you should be done!

## Contributing

See [CONTRIBUTING.md](./CONTRIBUTING.md).

Development-focused discussion about Rustlings happens in the [**rustlings** stream](https://rust-lang.zulipchat.com/#narrow/stream/334454-rustlings)
on the [Rust Project Zulip](https://rust-lang.zulipchat.com). Feel free to start a new thread there
if you have ideas or suggestions!

## Contributors ✨

Thanks goes to the wonderful people listed in [AUTHORS.md](./AUTHORS.md) 🎉
### **conversions**
- [using_as](exercises/conversions/using_as.rs)
- [from_into](exercises/conversions/from_into.rs)
- [from_str](exercises/conversions/from_str.rs)
- [try_from_into](exercises/conversions/try_from_into.rs)
- [as_ref_mut](exercises/conversions/as_ref_mut.rs)
4 changes: 1 addition & 3 deletions exercises/clippy/clippy1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
// check clippy's suggestions from the output to solve the exercise.
// Execute `rustlings hint clippy1` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

use std::f32;

fn main() {
let pi = 3.14f32;
let pi = f32::consts::PI;
let radius = 5.00f32;

let area = pi * f32::powi(radius, 2);
Expand Down
6 changes: 2 additions & 4 deletions exercises/clippy/clippy2.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// clippy2.rs
// Execute `rustlings hint clippy2` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

fn main() {
let mut res = 42;
let option = Some(12);
for x in option {
res += x;
if let Some(x) = option {
res += x
}
println!("{}", res);
}
16 changes: 4 additions & 12 deletions exercises/clippy/clippy3.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
// clippy3.rs
// Here's a couple more easy Clippy fixes, so you can see its utility.

// I AM NOT DONE

#[allow(unused_variables, unused_assignments)]
fn main() {
let my_option: Option<()> = None;
if my_option.is_none() {
my_option.unwrap();
}

let my_arr = &[
-1, -2, -3
-4, -5, -6
];
let my_arr = &[-1, -2, -3, -4, -5, -6];
println!("My array! Here it is: {:?}", my_arr);

let my_empty_vec = vec![1, 2, 3, 4, 5].resize(0, 5);
let mut my_empty_vec = vec![1, 2, 3, 4, 5];
my_empty_vec.clear();
println!("This Vec is empty, see? {:?}", my_empty_vec);

let mut value_a = 45;
let mut value_b = 66;
// Let's swap these two!
value_a = value_b;
value_b = value_a;
std::mem::swap(&mut value_a, &mut value_b);
println!("value a: {}; value b: {}", value_a, value_b);
}
10 changes: 4 additions & 6 deletions exercises/conversions/as_ref_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
// and https://doc.rust-lang.org/std/convert/trait.AsMut.html, respectively.
// Execute `rustlings hint as_ref_mut` or use the `hint` watch subcommand for a hint.

// I AM NOT DONE

// Obtain the number of bytes (not characters) in the given argument
// Add the AsRef trait appropriately as a trait bound
fn byte_counter<T>(arg: T) -> usize {
fn byte_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().as_bytes().len()
}

// Obtain the number of characters (not bytes) in the given argument
// Add the AsRef trait appropriately as a trait bound
fn char_counter<T>(arg: T) -> usize {
fn char_counter<T: AsRef<str>>(arg: T) -> usize {
arg.as_ref().chars().count()
}

// Squares a number using as_mut(). Add the trait bound as is appropriate and
// implement the function body.
fn num_sq<T>(arg: &mut T) {
???
fn num_sq<T: AsMut<u32>>(arg: &mut T) {
*arg.as_mut() *= *arg.as_mut()
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit aecb6c4

Please sign in to comment.