Lab: Rust Mains
In today's lab, you'll write several Rust programs and practice with I/O, loops, Result
, conditional statements, iterators, string manipulation, and command-line arguments.
Ordinal
Define in ordinal.rs
a function named ordinal
that accepts a u32
parameter and returns a String
of the parameter's ordinal representation. For example: ordinal(112)
returns "112th"
, ordinal(3)
returns "3rd"
, and ordinal(62)
returns "62nd"
. Be careful on this. The decision logic has some nuance.
Define also a main
function that prints out the ordinal strings for numbers 1 through 130. Make sure the output matches your expectations.
Verb
Spanish verbs end in -ar, -er, or -ir. They conjugate according to their subject and tense. Regular verbs follow a formulaic conjugation that depends on the suffix. The verbs hablar, comer, and vivir are representative of their suffix classes and have these conjugations for the present tense:
subject | hablar (to speak) | comer (to eat) | vivir (to live) |
---|---|---|---|
I | hablo | como | vivo |
you | hablas | comes | vives |
he, she, it | habla | come | vive |
we | hablamos | comemos | vivimos |
they | hablan | comen | viven |
Write in verb.rs
a main
function that receives an unconjugated verb (an infinitive) as a command-line argument and conjugates it according to the regular verb conjugation rules illustrated in this table. It prints the five conjugations. Ensure that your program works for any regular verb, not just these three.
CSV Merge
Write in csvmerge.rs
a main
function that accepts three paths as command-line arguments. The first two are paths to existing comma-separated value (CSV) files, and the third is a path to an output file. It reads in the two CSV files and concatenates each line \(i\) of the first file with line \(i\) of the second—with a separating comma. The concatenated lines are written to the output file, erasing whatever content was previously in the file. Be careful!
Suppose the first file has these lines describing pizzas and their unique ingredient:
Sopressata,salami
Margherita,basil
Bella Bianca,mushrooms
The Don,meatballs
Sopressata,salami Margherita,basil Bella Bianca,mushrooms The Don,meatballs
And the second file has these lines describing quantities and unit costs:
1,13
2,13.50
1,14.50
3,14.50
1,13 2,13.50 1,14.50 3,14.50
The output file will contain this text:
Sopressata,salami,1,13
Margherita,basil,2,13.50
Bella Bianca,mushrooms,1,14.50
The Don,meatballs,3,14.50
Sopressata,salami,1,13 Margherita,basil,2,13.50 Bella Bianca,mushrooms,1,14.50 The Don,meatballs,3,14.50
You can read in a file all at once with a statement like this:
let text = std::fs::read_to_string(path).unwrap();
let text = std::fs::read_to_string(path).unwrap();
Consider adopting this implementation: get two line iterators and then call the zip
function to produce an iterator that iterates through both simultaneously. Use methods of File
to output the merged lines. Interestingly, there is no close
method. Files close automatically when they go out of scope.
Submit
To receive credit for this lab, you must submit all three of your .rs
files on Canvas by Monday noon. Late labs or forgot-to-submits are not accepted because Monday at noon is when your instructor has time to grade.