Example (with solution) for the Rust Fundamentals Workshop

Overview

Workshop Repo: Rust Fundamentals

NOTE: This is Work-In-Progress! Please check for updates a day before the workshop.

This Github repository contains all the source code and examples for the first homework assessment

Install Rust

Rustup provides you with all the software to compile and run Rust applications, e.g.

  1. Cargo - build tool and package manager
  2. rustfmt - Auto-formatting tool for Rust code
  3. clippy - Linting for common mistakes

and many more. Rustup also allows you to install different compile targets and multiple toolchains, as well as keeping your toolchains up to date.

After installing, you should have a set of new command line tools available.

Verify your Rust installation:

  1. Open a Terminal/Shell/Command Line of your choice
  2. Check out this repo
  3. Navigate to this repository
  4. Enter
$ cargo build
  1. Open your browser at https://localhost:3000

Recommended Editor

During the workshop, we will use Visual Studio Code as editor. It's free, fast and very extensible. Making yourself familiar with VS Code is highly recommended.

However, working with VS Code is not required. If you have a preferred editor with Rust support you're more productive with, please feel free to use whatever you like. What we highyly recommend though, is checking if your editor has support for Rust analyzer.

Recommended VS Code Extensions

To work effeciently, please install a couple of extensions that help you developing Rust. Note: Please don't install the recommendend Rust extension. It's outdated and the community decided to move to other tools. You can search and install VS Code extensions through the menu on the side

We recommend the following extensions:

  • rust-analyzer. This is the main extension for Rust development, with the best language support available. Note: This extension is also available for other IDEs and editors, check out their website

  • crates. This extension helps installing dependencies from crates.io

  • Better TOML. TOML is the format that the dependency manager Cargo uses to manage dependencies. This extension helps formatting and editing TOML files

  • CodeLLDB. All Rust code is compiled against LLVM. This extension helps debugging LLVM code inside VS Code

  • Error Lens. Inline errors

Teaching goals

  • Rust fundamentals: loops, structs, conditionals, APIs
  • Basic ownership and borrowing
  • Traits: Generics, trait bounds
  • Error handling

Tasks

Our goal is to build a CLI tool that takes filenames as arguments and counts words, lines, bytes, and characters. It will display the sum of all words, lines, bytes and characters to stdout.

Those are the four exercises.

Exercise 1 (ex1.rs): count

  • Implement a count function that takes an Cursor and returns a FileInfo struct.
  • Implement the FileInfo
  • Count the number of lines
  • Count all words (e.g. ignore whitespace)
  • Count bytes and characters

Exercise 2: error handling and trait bounds

  • Extend your software to be able to handle everything hat implements BufRead (Cursor, BufReader)
  • There are some situations that require error handling. Make sure of proper error handling through error propagation.

Exercise 3 (ex2.rs): traits

  • Implement the Sum trait to collect the totals over an iterator of FileInfo.
  • Implement the Display trait to put out all parts, taking a width of 8 characters per part

Exercise 4: CLI app

  • Make your app runable as CLI application (std::env). Parse the command line arguments, call your functions, and display the results.

Extensions

  • Structure your app to allow for different displays, e.g. show only words, only characters, a mixture of all four pieces.
  • Extend your app to show the filename as well
  • Use a library like clap to make a good CLI interface
  • Try to parallelize across threads (rayon) and see if there's any difference in execution
You might also like...
clone of grep cli written in Rust. From Chapter 12 of the Rust Programming Language book

minigrep is a clone of the grep cli in rust Minigrep will find a query string in a file. To test it out, clone the project and run cargo run body poem

Rust-blog - Educational blog posts for Rust beginners

pretzelhammer's Rust blog 🦀 I write educational content for Rust beginners and Rust advanced beginners. My posts are listed below in reverse chronolo

The ray tracer challenge in rust - Repository to follow my development of
The ray tracer challenge in rust - Repository to follow my development of "The Raytracer Challenge" book by Jamis Buck in the language Rust

The Ray Tracer Challenge This repository contains all the code written, while step by implementing Ray Tracer, based on the book "The Ray Tracer Chall

Learn-rust-the-hard-way - "Learn C The Hard Way" by Zed Shaw Converted to Rust

Learn Rust The Hard Way This is an implementation of Zed Shaw's Learn X The Hard Way for the Rust Programming Language. Installing Rust TODO: Instruct

Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]
Learn to write Rust procedural macros [Rust Latam conference, Montevideo Uruguay, March 2019]

Rust Latam: procedural macros workshop This repo contains a selection of projects designed to learn to write Rust procedural macros — Rust code that g

The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language.

rcc The Rust Compiler Collection is a collection of compilers for various languages, written with The Rust Programming Language. Compilers Language Co

Integra8 rust integration test framework Rust with a focus on productivity, extensibility, and speed.

integra8 Integra8 rust integration test framework Rust with a focus on productivity, extensibility, and speed. | This repo is in a "work in progress"

Neofetch but in Rust (rust-toml-fetch)
Neofetch but in Rust (rust-toml-fetch)

rtfetch Configuration Recompile each time you change the config file logo = "arch.logo" # in src/assets. info = [ "", "", "yellow{host_n

Rust Sandbox [code for 15 concepts of Rust language]

Rust-Programming-Tutorial Rust Sandbox [code for 15 concepts of Rust language]. The first time I've been introduced to Rust was on January 2022, you m

Owner
Stefan Baumgartner
Stefan Baumgartner
HackWasm Workshop Starter, Checkpoints and Instructions

Osmosis Swaprouter Worskhop This is a workshop for building an Osmosis Swap Router CosmWasm contract. The original contract repository is located here

Roman 6 Oct 3, 2022
An example of a Kubernetes operator implemented in Rust

Rust Kubernetes operator example A Kubernetes operator built on top of kube-rs project. There is an explanatory article available. Steps to run on Lin

Pavel Pscheidl 91 Jan 1, 2023
ARM TrustZone-M example application in Rust, both secure world side and non-secure world side

ARM TrustZone-M example application in Rust, both secure world side and non-secure world side; projects are modified from generated result of cortex-m-quickstart.

null 44 Dec 4, 2022
A Domain Driven Design example application in Rust.

Rust Domain Driven Design Example rust-ddd Rust Domain-Driven-Design (DDD) Summery This repository is used to present how I find implementing DDD in R

Behrouz R.Farsi 6 Nov 15, 2022
Complete code for the larger example programs from the book.

Code Examples for Programming Rust This repository contains complete code for the larger example programs from the book “Programming Rust”, by Jim Bla

Programming Rust 670 Jan 1, 2023
Leetcode Solutions in Rust, Advent of Code Solutions in Rust and more

RUST GYM Rust Solutions Leetcode Solutions in Rust AdventOfCode Solutions in Rust This project demostrates how to create Data Structures and to implem

Larry Fantasy 635 Jan 3, 2023
Simple autoclicker written in Rust, to learn the Rust language.

RClicker is an autoclicker written in Rust, written to learn more about the Rust programming language. RClicker was was written by me to learn more ab

null 7 Nov 15, 2022
Rust programs written entirely in Rust

mustang Programs written entirely in Rust Mustang is a system for building programs built entirely in Rust, meaning they do not depend on any part of

Dan Gohman 561 Dec 26, 2022
Rust 核心库和标准库的源码级中文翻译,可作为 IDE 工具的智能提示 (Rust core library and standard library translation. can be used as IntelliSense for IDE tools)

Rust 标准库中文版 这是翻译 Rust 库 的地方, 相关源代码来自于 https://github.com/rust-lang/rust。 如果您不会说英语,那么拥有使用中文的文档至关重要,即使您会说英语,使用母语也仍然能让您感到愉快。Rust 标准库是高质量的,不管是新手还是老手,都可以从中

wtklbm 493 Jan 4, 2023
A library for extracting #[no_mangle] pub extern "C" functions (https://docs.rust-embedded.org/book/interoperability/rust-with-c.html#no_mangle)

A library for extracting #[no_mangle] pub extern "C" functions In order to expose a function with C binary interface for interoperability with other p

Dmitrii - Demenev 0 Feb 17, 2022