Recap
Characters are stored in a string using the UTF8 encoding. That means that a single character (codepoint) can take several bytes. For instance, the character ฤ is written as 2 bytes: [196, 131]. The character ๐ถ is written as 4 bytes: [240, 159, 142, 182].
To iterate over the characters of a string, use .chars():
let s = "abc";
for i in s.chars() {
println!("{}", i);
}
Reading and writing text files example
use std::{io, fs};
fn do_stuff() -> Result<(), io::Error> {
let s = fs::read_to_string("src/main.rs")?;
fs::write("copy.rs", &s)?;
Ok(())
}
Problems
For each problem, make sure to test several success and failure cases.
Folder name: lab04
P1
Read a text file. Calculate and print its longest line considering the number of bytes, and the longest line considering the number of actual characters.
Example input:
strings are fun
๐๐ถ๐๐๐๐๐โ๐
rust
supercalifragilisticexpialidocious
P2
Implement the ROT13 cipher. This cipher will rotate each ASCII letter by 13. Be mindful that lowercase letter transform into lowercase letters, and uppercase letters into uppercase letters.
If any non-ASCII character is encountered, print an error message and stop. Read and write the text however you want.
P3
Have a list of abbreviations (this can be hardcoded):
pentru pt
pentru ptr
domnul dl
doamna dna
Read a phrase from a file and replace any word that is an abbreviation into its correct form. Assume that the phrase only contain spaces for delimitations.
Example:
Am fost la dl Matei pt cฤ m-a invitat cu o zi รฎnainte
=>
Am fost la domnul Matei pentru cฤ m-a invitat cu o zi รฎnainte
P4
Read the hosts file from your system (Windows: C:\Windows\System32\drivers\etc\hosts, Unix: /etc/hosts). Split the lines, ignore the lines that start with #, and print the content in the format below. Take into considerations only the first two text columns of each entry.
localhost => 127.0.0.1
...
Note: the file might be fully commented if you're running a security product. Try with a file written in another location if that is the case.
Bonus
Generate a 4GiB file with ascii text by repeating some text as many times as necessary.
Go back to P2 and use the app from there to encrypt the 4GiB file. Note down how much time it takes. Now optimize the code as much as you can so the time gets as small as possible.
Timing code example:
use std::time::Instant;
fn do_stuff() {
for _ in 0..1_000_000 {
print!("");
}
}
fn main() {
let start = Instant::now();
do_stuff();
println!("{:?}", start.elapsed());
}