CSE 5402S: Studio 17

Foreign Functions and Variables


In today's studio you will explore how functions and variables written in C can be used in Rust programs. Of particular interest is how the libc crate can be used to facilitate interactions with the standard C library.


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

    Change into the src directory within that package and in the main.rs file that it contains, modify the main function so that it uses env::args() to check how many command line arguments were passed to it (including the name of the program itself) and uses the println! macro to print out that number.

    Compile your program, and run it with and without additional command line arguments. As the answer to this exercise please show the code for the main function and the command line and the output that was produced by each run.

  3. Modify the main function so that after it prints out the number of command line arguments passed to it, it then uses env::args() and the println! macro to print out each argument (including the name of the program itself).

    Compile your program, and run it with and without additional command line arguments. As the answer to this exercise please show the code for the main function and the command line and the output that was produced by each run.

  4. Modify the main function so that it stores the number of command arguments in a variable of type std::os::raw::c_int and then uses the println! macro to print out that variable.

    Compile your program, run it with the same command lines as in the previous exercise, and confirm that the same output as in the previous exercise was produced in each case. As the answer to this exercise please show the code for the main function.

  5. Add the line libc = "0.1.0" to the [dependencies] section of the Cargo.toml file in the package you created for this studio.

    In your main.rs file, above the main function, add the following declarations:

    use std::ffi::CString;

    extern crate libc;

    extern "C" { pub fn printf(format: *const libc::c_char, ...) -> libc::c_int; }

    Modify the main function so that instead of using the println! macro to print out the c_int variable, it calls the foreign printf function from inside of an unsafe block to do that.

    To make this work, you'll need to convert the format string that you had used with the println! macro, into one that printf can use, as follows: (1) add "\n" to the end of it so that there will be a line break when it's printed, (2) in it replace "{}" with "%i", (3) wrap it in a variable that's initialized using CString::new, and (4) pass a call to .as_ptr() on the CString variable, and also the c_int variable, into a call to printf.

    Compile your program, run it with the same command lines as in the previous exercise, and again confirm that the same output as in the previous exercise was produced in each case. As the answer to this exercise please show the code for the updated main function.

  6. In your main function, remove the unsafe block from around the call to printf.

    Try to compile your program and as the answer to this exercise please (1) show the error that occurred and (2) explain briefly why it occurred.

  7. Modify your main function by re-adding the unsafe block, moving the code that prints out the command line arguments into it, and modifying that code so that it also calls printf instead of using the println! macro.

    To make this work, you will need to (1) create a new CString from "%s\n" to use as a format string, and (2) for each of the arguments in env::args() create a new CString from it and then pass calls to .as_ptr() on both the format and argument CStrings into a call to printf.

    Compile your program, run it with the same command lines as before, and again confirm that the same output as in the exercise before the previous one was produced in each case. As the answer to this exercise please show the code for the updated main function.

  8. Modify your main function so that instead of surrounding all of the output code (including iterating through the arguments) with a single unsafe block, you only wrap each call to printf in its own narrow unsafe block.

    Compile your program, run it with the same command lines as before, and again confirm that the same output was produced in each case. As the answer to this exercise please show the code for the updated main function.

Things to Turn In:

For this studio, please turn in the following:


Page updated Friday July 17, 2026, by Chris Gill.