Code Monkey home page Code Monkey logo

rusty-wc's Introduction

rusty-wc ๐Ÿšพ

Learn Rust hands-on by adding a feature to a rusty wc clone.

This exercise was developed for Orca Security's Rust workshop, but it's open source under the license. Feel free to use it for your own workshops or personal learning, and contribute back if you find any issues or improvements!

Rusty Orca

What's the exercise?

Learn some rust by practicing a "real-life" example of adding a feature to this CLI. The CLI we're basing off of is Good ol' wc. In this repo, you'll find you already have a basic implementation of wc in Rust. It includes support for three flags: -l, -w, and -c, which count lines, words, and characters, respectively.

Your task

Step 0 // Get familiar with the code and environment

  1. Clone the repository, follow the Installation instructions, and open the project in your favorite IDE (see IDEs).
  2. Run the tests to make sure everything is working (see Testing).
  3. Run the program with cargo run -- -h to see the help message.
  4. Run the program with cargo run -- LICENSE CONTRIBUTING.md to see the results.
  5. Skim through the code to understand how it works, focusing on ./src/main.rs. Dependencies are managed in Cargo.toml.

Step 1 // Add a new feature

The feature you'll implement is adding a -f flag - which will count frequency of words in the input files, and print the top 10 most frequent words. The -f flag should be mutually exclusive with the other flags. If multiple files are provided, the frequency should be calculated for all files combined.

Bonus points

Step 2 // Add tests to your feature

If you're a TDD beast, you might have done this already! Add tests for the new feature. For the LICENSE file, you can use these results as a reference:

 309 the
 208 of
 174 to
 165 a
 131 or
 102 you
 89 that
 86 and
 72 this
 70 in

And for both LICENSE and CONTRIBUTING.md:

 318 the
 208 of
 182 to
 168 a
 131 or
 104 you
 93 that
 88 and
 72 this
 70 for

Step 3 // Benchmarking and optimization

Use Criterion to benchmark your -f implementation. Then, re-implement it with parallelism using Rayon, and with the benchmark, compare the performance of the parallel implementation with the sequential one.

Installation

Follow the Rust installation guide.

The exercise was written with rustc 1.79.0-nightly. Check yours with rustc --version and if needed, run:

rustup update

IDEs

NOTE: We recommend initially disabling CoPilot completions while working on the exercise, as experiencing friction with the syntax is good for learning.

Once you've experienced some friction with the syntax, re-enable CoPilot - but be mindful of the suggestions it gives you, as it might not always be the best way to solve the problem. Make sure to thouroughly read the code it suggests before using it, and make sure you understand it.

VSCode

In VSCode, install the Rust Analyzer extension, and make sure to install a debugger as well. Try to debug one of the tests to make sure everything works.

There are a few recommended settings to tweak:

{
  "[rust]": {
    "editor.defaultFormatter": "rust-lang.rust-analyzer"
  },
  "rust-analyzer.testExplorer": true
}

Vim

Install the official rust.vim plugin.

JetBrains IDEs

For JetBrains IDEs, consider RustRover. It's still in preview at the time of writing, but it should be fully released by Sep 2024.

Testing

There are two testing suites: unit tests in the main.rs file and integration tests in the tests/ directory. To run the tests, use:

cargo test -- --nocapture

rusty-wc's People

Contributors

thecoreman avatar borisorca avatar roin-orca avatar

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.