Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exercise: concatenate file content

  • TODO

Write a function that received the name of two files and returns the content of the two files concatenated in both directions.

e.g. If file 1 contains "cat" and file 2 contains "dog" then return "catdog" and "dogcat"

fn main() {
    let res = concatenate("cat.txt", "dog.txt");
    println!("{:?} {:?}", res.0, res.1);
}

fn concatenate(file1: &str, file2: &str) -> (String, String) {
    let data1 = std::fs::read_to_string(file1).unwrap();
    let data1 = data1.trim_end();
    let data2 = std::fs::read_to_string(file2).unwrap();
    let data2 = data2.trim_end();
    (format!("{data1}{data2}"), format!("{data2}{data1}"))
}
  • TODO: This works, but let's also implement without format!
  • TODO: Return Result