In today's studio you will explore Rust's support for reading and writing data via readers, writers, and buffered readers and writers. We will focus on input and output from and to files as well as from and to standard input and output streams, though the ideas explored in today's studio pertain to networked sockets and other abstractions as well.
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.
As the answer to the first exercise, list the names of the people who worked together on this studio.
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., rustio
).
Change into the src
directory within that package and in
the main.rs
file that it contains modify the main
function
so that it declares three variables of different types and uses the
println!
macro to print out all three variables' names and values
on a single line. Compile and run your program, and as the answer to this exercise
please show the output that was produced.
Modify your program so that instead of using the println!
macro, it
issues the statement
use std::io::Write;and uses the
writeln!
macro to print the formatted out to the
standard output stream in a thread-safe manner, using the writer obtained by calling
std::io::stdout().lock()
as the first argument to that macro, but do
not use ?
or otherwise process the Result
from that call.
Compile and run your code and show the output that was produced.
Modify your program so that the Result
returned by the
writeln!
macro is stored in a variable and an appropriate
match
expression is then used to evaluate that Result
.
Compile and run your code and show (1) the output that was produced and (2) the code that you wrote for this.
Modify your program so that it takes a single command line argument with the name
of a file, prints out the name of the file, tries to open that file in a buffered
reader (printing out an error message if the attempt fails), and if successful uses
that reader's lines()
method to iterate over and print out each of the
lines from the file (note that you will need to use a match expression to
obtain each line of text from the Result
that is returned).
Compile your program and run it with text identifying a file that exists (e.g.,
"../Cargo.toml"
), and then with the name of a file that does not (e.g.,
just "Cargo.toml"
without the directory prefix). As the answer to this
exercise please show the output your program produced for each of those runs.
Modify your program so that it only prints out the non-blank lines (ones that contain at least one non-whitespace character) that were read from the file. Run the program with text that identifies a file that exists and that has at least some blank lines. As the answer to this exercise please show (1) the output the program produced, and (2) the code you wrote for this exercise and the previous one.
For this studio, please turn in the following:
Page posted Friday November 1, 2024, by Chris Gill.