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.
Diff
Write in diff.rs
a main
function that accepts two paths as command-line arguments, reads in the two files, and prints out how they differ. You can read in a file all at once with a statement like this:
let text = std::fs::read_to_string(path).unwrap();
Compare the files character by character. When two characters differ, output the mismatch in some way. Here's one way:
I got rickets for the Snift concern.
I got tickets for the Swift concert.
^ ^ ^
Consider adopting this implementation: get two character iterators and then call the zip
function to produce an iterator that iterates through both simultaneously.
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.