Crate getownedlab

Crate getownedlab 

Source
Expand description

§98-008: Intro to the Rust Programming Language

§Get Owned Lab

The goal of this homework is to familiarize yourself with Rust’s system of Ownership.

Lecture was heavily based on Chapter 4 of the Rust Book. We strongly suggest that you make sure you understand the rules of Ownership before attempting this homework, and this text is a great place to reference!

This homework is a bit shorter than the previous one, mainly so you can have extra time to read through the book and stew with the concepts a bit.

If you find yourself getting stuck, it might be good to take a small break and go over the borrow checker’s rules.

Remember to always read the error messages that the compiler gives you, and as always, we encourage you to reach out for help!

§Part 1: Slices

Like the previous homework, you will modify 3 files that don’t compile by adding, removing, or reorganizing code to make them compile! The process should be identical to the previous homework, except the exercises are now specific to slices.

When you are done, you should be able to run cargo test with all of the modules (pub mods) in src/slices/mod.rs uncommented.

§Part 2: Move Semantics

Like the previous section, you will modify 5 files to make them compile. The first 3 exercises involve the Vec type, which is an owned type.

For this homework, all you need to know is the vec![] macro and the push method on vectors. Recall the vec![] creates a new vector with elements, and push appends an element onto the end of a vector.

§Part 3: Strings

In this section, you’ll be working with String and &str instead of Vec. Recall that String is an owned type and &str is borrowed type.

The first three exercises in src/strings are just like the previous section, where you need to make some changes for the functions to compile.

A useful thing to know here is that a &String can be deref coerced into a &str when passed as a function parameter (you can imagine it turning into a different type as it enters the function).

Other than that, just remember to always read what the compiler tells you (it may or may not give away the answer)!

For strings4.rs, you will need to look at some real documentation.

You could just google the answer, but it would be good to familiarize yourself with official Rust documentation! Linked here is the documentation for str, which might be useful.

There’s a search bar at the top of the documentation that you should make use of. Try searching for a method called trim there!

§Submission

§Formatting and Style

The autograder will run these two commands on your code:

cargo clippy && cargo fmt --all -- --check

If the autograder detects any errors from the command above, you will not be able to receive any points. This may seem strict, but we have decided to follow standard best practices for Rust.

By following Rust’s style guidelines, you ensure that anybody reading your code (who is familiar with Rust) will be able to easily navigate your code. This can help with diving into an unfamiliar code base, and it also eliminates the need for debate with others over style rules, saving time and energy.

See the official guidelines for more information.

§Unix

If you are on a unix system, we will try to create a handin.zip automatically for you, but you will need to have zip already installed.

If you do not have zip installed on your system, install zip on your machine or use the CMU Linux SSH machines. If you need help with this, please reach out to us!

Once you have zip installed, we will create the handin.zip automatically for you (take a peek into build.rs file if you’re interested in how this works!).

Once you have the handin.zip file, submit it (and only the zip) to Gradescope.

§Windows

If you are on a windows system, you can zip the src/ folder manually and upload that to Gradescope.

Note that you don’t need to name it handin.zip, you can name it whatever you’d like.

§Collaboration

In general, feel free to discuss homeworks with other students! As long as you do not copy someone else’s work, any communication is fair game.

All formal questions should be asked on Piazza. Try to discuss on Piazza so that other students can see your questions and answers as well!

You can also discuss on Discord, but try to keep any technical questions on Piazza.

§Feedback

We would like to reiterate that you should let us know if you spent anywhere in significant excess of an hour on this homework.

In addition, Rust has a notoriously steep learning curve, so if you find yourself not understanding the concepts, you should reach out to us and let us know as well — chances are, you’re not the only one!

Modules§

move_semantics
All examples taken from rustlings exercises and practice.rs
slices
All examples taken from practice.rs
strings
All examples taken from rustlings exercises