Contents

Homework 1

 Back to syllabus

Toolchain

If you haven’t setup the Rust toolchain, head to https://rustup.rs. The website detects your operating system and gives you instruction on how to install in your system. For example, on Linux and macOS, the following is the suggested installation method:

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

You can directly proceed with the default installation option when you see prompts. By default, the Rust toolchain will be installed in .cargo/ and .rustup/ folders in your home directory.

Alternatively, you can also use the package manager on your operating system. Just make sure to use a recent version to prevent potential breakage. We will be testing all the homework using Rust 1.66.0.

Get started

Starter code

In this homework, we will get familiar with the basic syntax, and exercise on mutability, ownership, borrowing, and lifetime. To get started, clone the starter code:

1
2
git clone https://osdi.dev/rust-jhu/hw1-starter.git hw1
cd hw1/

Now, open src/lib.rs file in your favorite code editor. Find all the unimplemented!() and replace with your implementation code as required by the comments.

Exercises

The requirement in the comments are written in markdown, and can be compiled into HTML (see more). Run the following command to compile the docs.

1
cargo rustdoc --open

The --open option tells cargo to open the HTML document in a browser, and you will see a local webpage opened in your browser.

Running

The repository is structured as a library-only project. It does not compile into any standalone executable files. To test the correctness of your code, run cargo test in the command line, or cargo test --doc if only testing the last exercise. You will see test framework output as below. It will show what cases have passed and failed, and also output the reason for the failed check.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
running 2 tests
test tests::test_mem_fib_borrow ... ok
test tests::test_mem_fib ... FAILED

failures:

---- tests::test_mem_fib stdout ----
thread 'tests::test_mem_fib' panicked at 'assertion failed: `(left == right)`
  left: `([0, 1], 1)`,
 right: `([0, 1, 1], 1)`', src/lib.rs:77:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    tests::test_mem_fib

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Requirement: In this homework, you should not:

  • use any unsafe code
  • add #![...] options to the beginning of the file or module
  • change the function signatures unless otherwise required by specific exercises

You can modify the test cases, and also add your own test cases.

Note: the official test cases on Gradescope might be different from the sample test cases in the starter code. Therefore, you may want to add your own test cases to test more exhaustively.

Submission

This homework is due on January 22nd, 2023 at 23:59pm.

Submit only the lib.rs file to Gradescope.