CSE 5402: Studio 8

Operators and Callable Types


In today's studio you will explore how traits are used to support the overloading of different kinds of operators in Rust, and how different callable types may be defined and used in your Rust programs.


Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.

As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then on the Canvas page for this studio assignment upload (1) a file containing your answers and (2) any code you produced while answering the exercises. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.

  2. Log in using ssh into shell.cec.wustl.edu using your WUSTL Key id and password, issue the qlogin command to get onto one of the Linux Lab machines, and then within the directory you created for this course, add a new directory for this studio.

    In that new directory, use the cargo new command to create a new package (named e.g., rustoverloading).

    Change into the src directory within that package and replace the main.rs file that it contains with your main.rs file from the (completed) code for the previous studio.

    Replace the contents of the main function with a declaration of a variable of the Car type and a statement that prints it out. Compile and run your program, and as the answer to this exercise please show the output that was produced.

  3. Declare a Position struct with two fields of type isize and derived implementations of Debug and any other traits that are needed. Provide an implementation for Position with a new function that takes two isize arguments and uses them to initialize the struct's fields.

    Modify the declaration of the Car struct and the implementations that access or modify its x and y positions, so that instead of having separate fields of type isize, they declare (and implement based on) a single field that is a Position struct. Compile and run your program, and as the answer to this exercise please show the output that was produced.

  4. Add the statement

    use std::ops::AddAssign;

    and below it provide an appropriate implementation of the AddAssign trait for the Position struct. Modify the implementation of the Mover trait for the Car struct so that it uses the += operator to update the Position of the Car using another Position object that is newly created using the appropriate values.

    Compile and run your program and confirm that the same output as in the previous exercise is produced. As the answer to this exercise please show your code that implements the AddAssign trait for the Position struct.

  5. At the bottom of your main function, have the car turn left, then advance, then turn right and advance again, after which the main function should again print out the state of the car. After that the main function should have the car perform a figure eight maneuver using the trait implementation from the previous studio. Compile and run your program, and examine the output it produced. As the answer to this exercise please (1) show the program's output and (2) explain why you think the car's movement is as it should be, with the new implementation based on overloading the += operator for the car's position.

  6. Add the statement

    use std::ops::SubAssign;

    and below it provide an appropriate implementation of the SubAssign trait for the Position struct. In the implementation of the Car struct add a public home method that uses the -= operator to reposition the car at the origin (position 0, 0). Then, at the bottom of the main function, call the car's home method and then again print out the car's state.

    Compile and run your program and as the answer to this exercise please show your code that implements the SubAssign trait for the Position struct and the output of the program that indicates that the home method is working correctly.

  7. Change into the top level directory for this studio, and in it use the cargo new command to create a new package (named e.g., rustclosures).

    Change into the src directory within that package and in the main.rs file that it contains modify the main function so that it (1) declares a variable of type f64 that is initialized to 0.0, and (2) prints out the result of taking the cosine of the sine of that variable (i.e., by chaining the f64::sin and f64::cos methods).

    Compile and run your program, and as the answer to this exercise please show your code and the output the program produced.

  8. Replace the part of your code that calls those methods with a call to a closure that captures the variable and calls those methods on it.

    Compile and run your program, and confirm that the same output as in the previous exercise was produced. As the answer to this exercise please show your modified code.

  9. Outside of your main function, declare a struct with three member fields: a variable of type f64, and two function-type fields with the signature of both fields taking a single f64 and returning an f64.

    In your main function, declare an instance of that struct, initialized with the variable you declared in the previous exercise, and the f64::sin and f64::cos methods.

    Modify the closure so that it captures the struct instance and uses its fields to perform the same calculation as in the previous exercise. Compile and run your program, and confirm that the same output as in the previous exercise was produced. As the answer to this exercise please show your modified code.

  10. Modify your struct declaration so that it has a single generic type parameter, and in that declaration replace all occurrences of f64 with that type parameter. In your main function modify the struct declaration so that it plugs in f64 for the type parameter.

    Compile and run your program, and confirm that the same output as in the previous exercise was produced. As the answer to this exercise please show your modified code.

  11. In your main program declare a mutable variable of type String, and initialize it to contain "Hello". Declare a closure that captures that variable, adds ", world!" to it, and returns the (modified) string variable as its result. Print out the string variable and then the result of calling the closure with it.

    Compile and run your program, as the answer to this exercise please show the code that you added for this exercise and the output produced by that code.

Things to Turn In:

For this studio, please turn in the following:


Page posted 17:50 Monday August 18th, 2025, by Chris Gill.