CSE 542S: Studio 13

Closures


In today's studio you will explore closures, functions, function pointers, and other callable types in Rust.


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., 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.

  3. 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.

  4. 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.

  5. 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 and the closure that uses it so that they both plug 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.

  6. 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 Friday October 18, 2024, by Chris Gill.