disemvowel-in-rust-bante created by GitHub Classroom

Overview

Rust Disemvowel

Rust tests

This is a simple lab where we'll use Rust to implement the disemvowel function that we covered in a previous C lab.

What is Rust?

Rust is a systems programming language designed to give the performance of C or C++ while providing the memory safety of garbage collected languages like Java. It does this through a very strict compiler that will refuse to compile code that is not memory safe. This means that rather than using Valgrind when you run your code like you did in C, the compiler will tell you if and where any potential memory problems exist when you compile your code. This is incredibly useful, but also can be frustrating as code that feels like it should work will refuse to compile over what might appear to be small errors.

Currently no other language promises memory safety while delivering the sort of performance seen in Rust, and in situations where speed and safety are the highest concern, Rust is becoming a more popular choice. Of all security bugs in Microsoft software, 70% of them are memory related, and Rust has been identified as a potential solution to these issues. Additionally, companies like Discord have been moving to Rust to gain the performance benefits of the language.

70% of Microsoft security bugs are related to memory safety

Rust Background

Running code

Cargo is Rust's package manager and build tool. You can run your code in the command prompt by using

     cargo run

And you can run the tests by using

     cargo test

Rust Variables

Types

The two data types you will be working with in this lab are Strings and Vectors.

Vectors are a variable sized collection that can store data of the same type; it is fairly similar to something like ArrayList in Java. You can not have a vector with both numbers and strings in it, but you can add or remove as many items as you want to a vector. You can create a vector in a few ways.

     //Initializes a vector containing 5 numbers.
     let v = vec![1, 2, 3, 4, 5];

     //Initializes an empty vector
     let v2 = Vec::new();

The String data type in Rust stores text data, but those are implemented as a vector of characters behind the scenes. You initialize a String in a number of ways, including.

     let s = String::from("Hello World");

Ownership

The main method of ensuring memory safety that Rust uses is ownership. Ownership means that only one variable can own a value at a given time. This is important because it means that when that owner goes out of scope (e.g., when the function returns), the value can be safely dropped. If two variables were pointing to that value we would not be able to drop the value as the other variable could still exist and need the value.

To get around this restriction, borrowing is used to allow a variable to have a read-only pointer or reference to a value. This allows a new variable to take the same value as another without taking ownership. This is often used to pass a value to a function without losing ownership of the original value.

In this snippet, for example, if we didn't use &s to indicate that calculate_length is borrowing the string (instead of taking ownership of it), then we wouldn't be able to print s after the call to calculate_length because we would have lost ownership over s.

    let s = String::from("Hello");
    let len = calculate_length(&s);
    println!("{}", s);

    fn calculate_length(str: &String) -> usize {
        s.len()
    }

Panics and Error Handling

Rust has a few methods of handling errors or missing values, but the one we will be focusing on is the Panic. When your code reaches an unrecoverable error, you can use the panic! macro to terminate the code with a custom error messsage such as:

     panic!("There's a problem!");

Disemvowel

As you've done in your prior lab, "Disemvoweling" is the act of removing all the vowels ('a', 'e', 'i', 'o', and 'u', both upper and lowercase) from a piece of text. This time your code will take an input and output file as arguments and write the disemvoweled contents of the input file to the output file. You will finish a few lines of code to read and write to files, then finish the disemvowel function. Make sure your code passes all cargo tests.

Your job here is to write a small Rust program that takes in two command line arguments, each of which are file names. The first should be the file containing the text to disemvowel, and the second should be the name of the file we'll write the disemvowelled text to.

The skeleton code is in src/main.rs, which includes a number of unit tests that (among other things) demonstrate Rust's ability to work with complex unicode text. See comments in that file for more guidance on what needs to be done to complete the lab.

Running cargo test should run the tests, and if you want to run it "by hand"

     cargo run this.txt that.txt

will run your program with the given command line arguments (this.txt and that.txt in the example).

The file input.txt has a variety of test text, and target_output.txt has the expected result of disemvowelling input.txt. If you run

     cargo run input.txt my_output.txt

then

     diff my_output.txt target_output.txt

should show you any differences between your output and the expected output.

Note: Because Rust can deal with a variety of languages via unicode, the question of what a vowel is, and how it should be handled is actually a little complicated and we don't attempt to deal with it completely here. There are some edge cases where an accented vowel shows up in Rust as two separate chars, and disemvowelling can remove the vowel but leave behind a dangling accent. We leave addressing that for another day.

You might also like...
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

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

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 标准库是高质量的,不管是新手还是老手,都可以从中

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

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

Comments
  • Feedback

    Feedback

    :wave:! GitHub Classroom created this pull request as a place for your teacher to leave feedback on your work. It will update automatically. Don’t close or merge this pull request, unless you’re instructed to do so by your teacher. In this pull request, your teacher can leave comments and feedback on your code. Click the Subscribe button to be notified if that happens. Click the Files changed or Commits tab to see all of the changes pushed to main since the assignment started. Your teacher can see this too.

    Notes for teachers Use this PR to leave feedback. Here are some tips: - Click the **Files changed** tab to see all of the changes pushed to `main` since the assignment started. To leave comments on specific lines of code, put your cursor over a line of code and click the blue **+** (plus sign). To learn more about comments, read “[Commenting on a pull request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/commenting-on-a-pull-request)”. - Click the **Commits** tab to see the commits pushed to `main`. Click a commit to see specific changes. - If you turned on autograding, then click the **Checks** tab to see the results. - This page is an overview. It shows commits, line comments, and general comments. You can leave a general comment below. For more information about this pull request, read “[Leaving assignment feedback in GitHub](https://docs.github.com/education/manage-coursework-with-github-classroom/leave-feedback-with-pull-requests)”.

    Subscribed: @biruk741 @biruk741 @DanteMillerDS

    opened by github-classroom[bot] 0
Owner
null
learn_rust_rustlings-qinyuhang created by GitHub Classroom

rustlings ?? ❤️ Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This inclu

The Learning&Training Hub of OS Kernel 2 Oct 22, 2022
Simple RESTful API in rust created with actix-web. (Routing, models, JWT auth).

rust-simple-api Simple RESTful API created with rust, actix-web, Diesel, JWT. Running application Manual Firstly generate a secret.key which will be u

null 2 Jul 30, 2022
A simple programming language, created for AP Computer Science

A simple programming language, created for AP Computer Science

Michelle S. 3 Sep 2, 2022
kickable - a crate created to answer the age old question

kickable kickable is a crate created to answer the age old question... "Can I Kick It?" This package is for showcase purposes only. What is a kickable

@defstream 4 Jan 12, 2023
An LR(1) parser generator and visualizer created for educational purposes.

.lr An LR(1) parser generator and visualizer created for educational purposes. Table of Contents What is an LR(1) parser? Why did you make this? How c

Umut 80 Oct 21, 2024
The second Rust implementation on GitHub of third-party REST API client for Bilibili.

Bilibili REST API The second Rust implementation on GitHub of third-party REST API client for Bilibili. Designed to be lightweight and efficient. It's

null 4 Aug 25, 2022
Tools for managing GitHub block lists

GitHub block list management Octocrabby is a small set of command-line tools and Octocrab extensions that are focused on managing block lists on GitHu

Travis Brown 97 Nov 3, 2022
Automatically deploy from GitHub to Replit, lightning fast ⚡️

repl.deploy Automatically deploy from GitHub to Replit, lightning fast ⚡️ repl.deploy is split into A GitHub app, which listens for code changes and s

Khushraj Rathod 78 Dec 22, 2022
example codes for CIS198 https://cis198-2016s.github.io/

CIS198: RUST 编程语言 学习背景 rust 和 c/c++/Java/Python/golang 不太一样 rust 学习曲线比较陡峭 rust 有很多颠覆认知的特性: 所有权,生命周期,借用检测 cargo 工具 函数式+命令式支持 视频讲解见 B站 课程大纲 Timeline Lec

Jinghui Hu 3 Apr 9, 2024
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