Giter Club home page Giter Club logo

rustlings's Introduction

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!

It is recommended to do the Rustlings exercises in parallel to reading the official Rust book, the most comprehensive resource for learning Rust πŸ“šοΈ

Rust By Example is another recommended resource that you might find helpful. It contains code examples and exercises similar to Rustlings, but online.

Getting Started

Installing Rust

Before installing Rustlings, you need to have Rust installed. Visit www.rust-lang.org/tools/install for further instructions on installing Rust. This will also install Cargo, Rust's package/project manager.

🐧 If you're on Linux, make sure you've installed gcc (for a linker).

Deb: sudo apt install gcc. Dnf: sudo dnf install gcc.

🍎 If you're on MacOS, make sure you've installed Xcode and its developer tools by running xcode-select --install.

Installing Rustlings

The following command will download and compile Rustlings:

cargo install rustlings
If the installation fails… (click to expand)
  • Make sure you have the latest Rust version by running rustup update
  • Try adding the --locked flag: cargo install rustlings --locked
  • Otherwise, please report the issue

Initialization

After installing Rustlings, run the following command to initialize the rustlings/ directory:

rustlings init
If the command rustlings can't be found… (click to expand)

You are probably using Linux and installed Rust using your package manager.

Cargo installs binaries to the directory ~/.cargo/bin. Sadly, package managers often don't add ~/.cargo/bin to your PATH environment variable.

The solution is to …

Now, go into the newly initialized directory and launch Rustlings for further instructions on getting started with the exercises:

cd rustlings/
rustlings

Working environment

Editor

Our general recommendation is VS Code with the rust-analyzer plugin. But any editor that supports rust-analyzer should be enough for working on the exercises.

Terminal

While working with Rustlings, please use a modern terminal for the best user experience. The default terminal on Linux and Mac should be sufficient. On Windows, we recommend the Windows Terminal.

If you use VS Code, the builtin terminal should also be fine.

Doing exercises

The exercises are sorted by topic and can be found in the subdirectory exercises/<topic>. For every topic, there is an additional README.md file with some resources to get you started on the topic. We highly recommend that you have a look at them before you start πŸ“šοΈ

Most exercises contain an error that keeps them from compiling, and it's up to you to fix it! Some exercises contain tests that need to pass for the exercise to be done βœ…

Search for TODO and todo!() to find out what you need to change. Ask for hints by entering h in the watch mode πŸ’‘

Watch Mode

After initialization, Rustlings can be launched by simply running the command rustlings.

This will start the watch mode which walks you through the exercises in a predefined order (what we think is best for newcomers). It will rerun the current exercise automatically every time you change the exercise's file in the exercises/ directory.

If detecting file changes in the exercises/ directory fails… (click to expand)

You can add the --manual-run flag (rustlings --manual-run) to manually rerun the current exercise by entering r in the watch mode.

Please report the issue with some information about your operating system and whether you run Rustlings in a container or virtual machine (e.g. WSL).

Exercise List

In the watch mode (after launching rustlings), you can enter l to open the interactive exercise list.

The list allows you to…

  • See the status of all exercises (done or pending)
  • c: Continue at another exercise (temporarily skip some exercises or go back to a previous one)
  • r: Reset status and file of an exercise (you need to reload/reopen its file in your editor afterwards)

See the footer of the list for all possible keys.

Continuing On

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.

Third-Party Exercises

Do you want to create your own set of Rustlings exercises to focus on some specific topic? Or do you want to translate the original Rustlings exercises? Then follow the link to the guide about third-party exercises!

Uninstalling Rustlings

If you want to remove Rustlings from your system, run the following command:

cargo uninstall rustlings

Contributing

See CONTRIBUTING.md πŸ”—

Contributors ✨

Thanks to all the wonderful contributors πŸŽ‰

rustlings's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rustlings's Issues

Line number is wrong in hint of exercise #1, Move Semantics

The line number is stated as 8 as to where the error occurs. The actual line is 10. I don't know if the line number is relative to the start of the function or expressed based on actual line numbering. Either case, the line numbering in the hint needs to be clearified or corrected.

Turn steveklabnik's LazyValue into an exercise

https://twitter.com/steveklabnik/status/694918807832391680
https://play.rust-lang.org/?gist=1612d02b48811005d9d0&version=stable

struct LazyValue {
    c: Box<Fn() -> i32>,
    v: Option<i32>, 
}

impl LazyValue {
    fn new(c: Box<Fn() -> i32>) -> LazyValue {
        LazyValue {
            c: c,
            v: None,
        }
    }
    fn value(&mut self) -> i32 {
        match self.v {
            Some(v) => v,
            None => {
                let v = (self.c)();
                self.v = Some(v);
                v
            },
        }
    }
}

fn main() {
    let f: Box<Fn() -> i32> = Box::new(|| {
        println!("closure called!");

        5
    });
    let mut v = LazyValue::new(f);

    println!("value: {}", v.value());
    println!("value: {}", v.value());
    println!("value: {}", v.value());
    println!("value: {}", v.value());
}

Wording ambiguous - what should be changed?

Greetings, to me, the wording of several exercises is ambiguous, for example in result1.rs:

Make this test pass!

Until looking at the hints, I was not sure if I should modify the code or the test in order to make it pass.

This is true for a few other exercises which are two-part (eg. main + a function or main + a macro or main + a test) as well (make it compile!). Which part should stay unchanged and what can be changed to make it work?

In a few exercises, this is made clear, but in others it is ambiguous.

Greetings

More information on running tests

When I got to the "tests" section, I wasn't sure how I was supposed to run them. Every other program, I could run rustc name.rs, but that doesn't work here. The link to the Rust Programming Language talks about making a new cargo project - is that what I am supposed to do? It seems like some more information on setting this up would be helpful. I think directions should be included (perhaps as a hint in tests1.rs)

if1.rs silently succeeds

The if1.rs rustling does not provide any feedback that you succeeded when you correctly implemented a solution.

Even though I understood why, it was a bit jarring to not seeing any form of output. Maybe especially so because I worked on if1.rs right after the test rustlings which give nice confirmation of their success and failure.

There are a few options to change the behavior of if1.rs if the current behaviour is deemed problematic. The ones I could think of are listed below.

  1. Make if1.rs a test. pro: This provides instant standardized feedback for both correct and incorrect implementations. con: It could muddle the lesson about `if.
  2. print some output. pro: user has confirmation if they are correct. con: It also could muddle the lesson about if.

I am willing an able to make changes to if1.rs and send a pull request for any improvements that are suggested. I am just opening this issue to discuss if it is a problem at all, and if it is a problem how to improve if1.rs.

Cargo configuration for easy local running

Currently there is no Cargo.toml for the project, which blocks a possibility to open it locally in an IDE.

Besides that would be very nice to have Cargo to run separate examples independently from each other.

I achieved that with placing all examples to $root/examples. That way Cargo auto-generates running targets like cargo run --example ex1. The problem with that approach was that I wasn't able to tell Cargo to look inside subfolders like $root/examples/variables/

Exercises involving derive

Ugh i don't really want it to just be "oh this is missing a #[derive(whatever)]", but I do want the usage of derives to be introduced somehow.... hmmmm...

More exercises on Primitives

Hi there,

The Primitives section has only a couple of exercises, and I think we could add a couple more. Here's a few suggestions:

fn main() {
    let a = // Create an array with 100 elements in it

    if a.len() >= 100 {
        println!("Wow, that's a big array!");
    } else {
        println!("Meh, I eat arrays like that for breakfast.");
    }
}
fn main() {
    let a = [1, 2, 3, 4, 5];

    let three_and_four = // Your slice here

    if three_and_four == [3, 4] {
        println!("Nice slice!");
    } else {
        println!("Not quite what I was expecting...");
    }
}
fn main() {
    let cat = ("Furry McFurson", 3.5);
    let /* your pattern here */ = cat;

    println!("{} is {} years old.", name, age);
}
fn main() {
    let numbers = (1, 2, 3);
    println!("The second number is {}", /* Use a tuple index to access the sec
ond element of numbers */);
}

I'd love to know what you think about these. Thanks!

Exercise for shared ownership between threads

Something like this, perhaps?

// Make this compile!

use std::rc::Rc;
use std::thread;

struct Temperature {
    degrees_in_celsius: i32,
}

fn main() {
    let temperature = Rc::new(Temperature { degrees_in_celsius: 20 });
    let temperature_shared = temperature.clone();
    thread::spawn(move || {
        println!("{:?} degrees farenheit", temperature_shared.degrees_in_celsius as f32 * 33.8);
    });
    println!("{:?} degrees celsius", temperature.degrees_in_celsius);
}

Exercise for shared mutability between threads

Something like this, maybe:

// Make this compile!

use std::sync::Arc;
use std::thread;

struct JobStatus {
    jobs_completed: u32,
}

fn main() {
    let status = Arc::new(JobStatus { jobs_completed: 0 });
    let status_shared = status.clone();
    thread::spawn(move || {
        for _ in 0..10 {
            thread::sleep_ms(500);
            status_shared.jobs_completed += 1;
        }
    });
    while status.jobs_completed < 10 {
        println!("waiting...");
        thread::sleep_ms(1000);
    }
}

Turn Jake's translations of Ruben's ADT exercises into rustlings exercises

Tests?

Could have tests that these fail with the intended error messages....?

OPEN THREAD: Exercises that you liked

I'm going to leave this issue open basically forever, please feel free to comment here if there were any exercises that you particularly liked! I'd love to know which ones are most useful to people so that i can make more exercises like those :)

Please specify which exercise you liked and any reasons you liked those better than others! ❀️

Exercise for closure ownership

Something like this, which yields an error message which explicitly calls out a possible solution:

// Make this compile!

use std::thread;

struct SpaceshipPart {
    weight: f32,
}

fn main() {
    let parts = vec!(SpaceshipPart { weight: 5.5 }, SpaceshipPart { weight: 0.9 });
    let num_parts = parts.len();
    thread::spawn(|| {
        for part in &parts {
            println!("part weighs: {}", part.weight);
        }
    });
    println!("handed off {} parts to another thread", num_parts);
}

Add Filename to source code

I really like the interactive session that I can get to from the main page, but I often forget which one I am working on. Having its name in the code would be convenient. I wouldn't mind pushing a change if you agree that this would be a good idea.

Code Audit

I did a quit audit of the rustling sources. This is also connected to #80. Generally the examples are looking pretty good/ up to date. Following is a more in-depth breakdown.

  • error_handling – all good
  • functions – good, maybe could use more complex examples (see below)
  • if – πŸ€·β€β™€οΈ It's if
  • macros – also good, but maybe some more complex examples could help
  • modules – one of the examples is okay, the other involves references to US politics that are both very western specific and will at some point also be woefully outdated.

Move semantics

  • move_semantics1.rs – This isn't a very great example in my opinion. Ownership of the vector is given to a function but the compilation error isn't actually about ownership but mutability. This is confusing and I don't think beginners would learn very much from it
  • move_semantics2.rs – This should be the first example (or slightly simplified)
  • move_semantics3.rs – Also not a great example, as it's a mutability issue in a function. Also, this issue was already solved in the first example.
  • move_semantics4.rs – This is okay

Moving on...

  • primitive_types – These are all good πŸ‘
  • standard_library_types – These are generally good. I think arc might be a bit hard, so maybe we can tweak it. The iterator examples are good, although maybe we want to add more. And rename the files to have sensible numbers?
  • strings – Nice examples πŸ‘
  • threads – It's a nice example but maybe a few more could be good
  • variables – All good πŸ‘

All in all there should be some work done but most of the examples are looking pretty good πŸ‘

Things that are missing/ might want to be added

These are more advanced concepts but it might still be nice for people to learn about them in a rustling kind-of fashion.

  • Some functions using impl Trait
  • Generic functions with ...<T>
  • An example with traits

Open Exercise Links in New Tab

I ran threw some exercises in my spare time, in chrome. The links don't open in a new tab, so maybe replacing them with HTML instead of pure markdown may be a good option so the resource can stay open.

Example:
<a href="http://example.com" target="_blank">http://example.com</a> = http://example.com

I can submit a PR if this gets "approved" by a maintainer πŸ˜„.

OPEN THREAD: Exercises that need more hints

I'm going to leave this issue open basically forever, please feel free to comment here if there was an exercise that could use more of a hint than what was there (if there were hints).

Please specify which exercise it is you're discussing, and if you have any ideas about what would make for a better hint, please let me know!

rustlings 2.0

Hello! There's been talks about taking this project in a new direction, and I'm excited to say that we have initial plans to move things forward.

A big inspiration for how to make a curriculum that is self-guided, but held together by a single thread is RubyKoans, which provides you with a .zip file containing a bunch of test-driven exercises that introduce you to basic Ruby syntax and concepts. It'd be nice if Rustlings could function the same way, ideally this would be the optimal workflow:

  1. Install Rust
  2. Download the exercises
  3. Everything else should be explained within the thing you download

This would make the project accessible to a lot more people, by making everything self-contained and available offline. Another goal should be to define a formal curriculum. Fortunately, we can probably take inspiration from Rust by Example and other similar projects that already exist, so I don't think this'll be that much of a problem.

Finally, I want to make this a cool project to contribute to! Let's work together to make this possible ⭐

There's some things we can do right now:

  • Go through the existing list of issues and sort out the ones that are irrelevant right now or will be irrelevant in the future
  • Introduce some consistent branding
  • Find more people! There's an IRC channel now: #rustlings on Mozilla IRC
  • Lay out plans for a website and update documentation to reflect the new approach
  • Figure out a new curriculum

(roughly sorted in order of important/priority)

If anyone has any suggestions, or wants to contribute or help out, please throw it at me in this issue!

Link to join IRC: https://client00.chat.mibbit.com/?server=irc.mozilla.org&channel=#rustlings

Exercises for using common standard library types

For example, Result:

// Make this test pass!

#[derive(PartialEq)]
struct PositiveNonzeroInteger(u64);

#[derive(PartialEq)]
enum CreationError {
    Negative,
    Zero,
}

impl PositiveNonzeroInteger {
    fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
        Ok(PositiveNonzeroInteger(value as u64))
    }
}

#[test]
fn test_creation() {
    assert!(PositiveNonzeroInteger::new(10).is_ok());
    assert!(PositiveNonzeroInteger::new(-10) == Err(CreationError::Negative));
    assert!(PositiveNonzeroInteger::new(0) == Err(CreationError::Zero));
}

Slices and ownership

Maybe a variation on this? http://is.gd/gdmetO

fn main() {
    let paths = vec!["foo", "bar", "baz"];
    let first_match = paths.iter().map(|path| {
        let mut absolute_path = std::env::current_dir().unwrap();
        absolute_path.push(path);
        absolute_path.as_path().to_str().unwrap()
    }).filter(|path| {
        path.starts_with("/")
    }).nth(0);

    println!("{:?}", first_match);
}

Get inspiration from koans

http://www.rubykoans.com/

There's something that isn't quite what I'm going for about koans, maybe that it's really contrived, maybe that the last time i tried them it... started too early? moved at a weird pace-- hitting really simple stuff but then moving on immediately?

Or maybe I just gave up on the koans too early?

I think I'm going for lots of similar-but-slightly-different exercises/situations. Like repeat one concept over and over until you get it, then move on to the next thing. Maybe.

OPEN THREAD: Exercises that you didn't like

I'm going to leave this issue open basically forever, please feel free to leave a comment if you find any particular exercises to be:

  • too confusing
  • too frustrating
  • not useful for learning
  • otherwise don't fit your expectations

Please include which exercise you're discussing and explain as best you can what you didn't like about it :)

Add jleedev's try!, From, and Error exercise

https://twitter.com/jleedev/status/694549253604184067
https://play.rust-lang.org/?gist=458a30c1943710f9f0a8&version=stable

// Make this compile! Scroll down for hints :)

use std::error;
use std::fmt;
use std::io;

// So many things could go wrong!
//
// Reading from stdin could produce an io::Error
// Parsing the input could produce a num::ParseIntError
// Validating the input could produce a CreationError
//
// How can we lump these errors into one general error?

fn read_and_validate(b: &mut io::BufRead) -> Result<PositiveNonzeroInteger, ???> {
    let mut line = String::new();
    b.read_line(&mut line);
    let num: i64 = line.parse();
    PositiveNonzeroInteger::new(num)
}

/*
fn read_and_validate(b: &mut io::BufRead) -> Result<PositiveNonzeroInteger, Box<error::Error>> {
    let mut line = String::new();
    try!(b.read_line(&mut line));
    let num: i64 = try!(line.trim().parse());
    let result = try!(PositiveNonzeroInteger::new(num));
    Ok(result)
}
*/

fn test_with_str(s: &str) -> Result<PositiveNonzeroInteger, Box<error::Error>> {
    let mut b = io::BufReader::new(s.as_bytes());
    read_and_validate(&mut b)
}

#[test]
fn test_success() {
    let x = test_with_str("42\n");
    assert!(x.is_ok());
    assert_eq!(PositiveNonzeroInteger(42), x.unwrap());
}

#[test]
fn test_not_num() {
    let x = test_with_str("eleven billion\n");
    assert!(x.is_err());
}

#[test]
fn test_non_positive() {
    let x = test_with_str("-40\n");
    assert!(x.is_err());
}

#[test]
fn test_ioerror() {
    struct Broken;
    impl io::Read for Broken {
        fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> {
            Err(io::Error::new(io::ErrorKind::BrokenPipe, "uh-oh!"))
        }
    }
    let mut b = io::BufReader::new(Broken);
    assert!(read_and_validate(&mut b).is_err());
}

// Sprinkle the try! macro around any Result<> values,
// which under the hood calls From::from
// on the error value to convert it to a
// boxed trait object, a Box<error::Error>,
// which is polymorphic.

#[derive(PartialEq,Debug)]
struct PositiveNonzeroInteger(u64);

#[derive(PartialEq,Debug)]
enum CreationError {
    Negative,
    Zero,
}

impl fmt::Display for CreationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str((self as &error::Error).description())
    }
}

impl error::Error for CreationError {
    fn description(&self) -> &str {
        match *self {
            CreationError::Negative => "Negative",
            CreationError::Zero => "Zero",
        }
    }
}

impl PositiveNonzeroInteger {
    fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
        if value == 0 {
            Err(CreationError::Zero)
        } else if value < 0 {
            Err(CreationError::Negative)
        } else {
            Ok(PositiveNonzeroInteger(value as u64))
        }
    }
}

#[test]
fn test_creation() {
    assert!(PositiveNonzeroInteger::new(10).is_ok());
    assert_eq!(Err(CreationError::Negative), PositiveNonzeroInteger::new(-10));
    assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0));
}

strings2 output is a bit misleading

I got the correct answer, and the program compiled - but the output of the program said:

Nope, that's not it.

Which led me to spend 10 minutes trying to figure out what I'd got wrong - before realising that this was the correct result!

Exercise for tuple structs

Specifically, this shows how to access/manipulate fields in tuple structs while requiring learners to fix privacy violations on said fields.

// Make this compile!

mod logging {
    pub struct Event(u64, String);

    const BaseEventId: u64 = 1000;
    pub fn create_event(id: u64) -> Event {
        Event(id + BaseEventId, "".to_owned())
    }
}

fn main() {
    let mut event = logging::create_event(13);
    event.1 = "Launched spaceship".to_owned();
    println!("{}", event.0);
}

Rust Playground can not run the code

Rust Playground compiles, but does not run examples!

To reproduce:

  1. I follow a link like https://play.rust-lang.org/?code=%2F%2F+Make+me+compile%21+Scroll+down+for+hints+%3A%29%0A%0Afn+main%28%29+%7B%0A++++x+%3D+5%3B%0A++++println%21%28%22x+has+the+value+%7B%7D%22%2C+x%29%3B%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Hint%3A+The+declaration+on+line+4+is+missing+a+keyword+that+is+needed+in+Rust%0A%2F%2F+to+create+a+new+variable+binding.%0A

  2. Compiler works and tells you to fix an issue

  3. Fix the example

  4. Hit "RUN"

  5. error 101:

thread '<main>' panicked at 'called Option::unwrap()on aNone value', ../src/libcore/option.rs:367 playpen: application terminated with error code 101

Environment:
Mac OS X, Google Chrome

Factorial exercise

I think a factorial exercise would help people learn about Ranges and .fold(). As an example solution:

fn factorial(num: u64) -> u64 {
    (1..num + 1).fold(1, |prod, x| prod * x)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.