book icon indicating copy to clipboard operation
book copied to clipboard

Exercise solutions

Open dycw opened this issue 3 years ago • 3 comments

Hi, this is a great tutorial!

As I am working through the book, I am trying the reader exercises — it would be great to have some solutions to refer to.

For example, Chapter 1.3 suggests one use a BufReader, so I have an attempt:

use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;

use clap::Parser;

#[derive(Parser)]
struct Cli {
    pattern: String,
    #[clap(parse(from_os_str))]
    path: std::path::PathBuf,
}

fn main() {
    let args: Cli = Cli::parse();

    println!(
        "You parsed pattern = {:?}, path = {:?}",
        &args.pattern, &args.path
    );

    let f = File::open(&args.path).expect("could not read file");
    let mut f = BufReader::new(f);

    for line in f.by_ref().lines() {
        if line.as_ref().unwrap().contains(&args.pattern) {
            println!("{}", line.as_ref().unwrap());
        }
    }
}

Now I'd like to see how to improve this.

Have I merely missed this somewhere in the book?

dycw avatar Feb 20 '22 08:02 dycw

No, we do not currently have examples of the exercises.

epage avatar Feb 21 '22 14:02 epage

Hello @epage

Great book. I am reading through it right now. Thank you for putting it together. May I take a crack at adding solutions to the exercise problems? I will start at section 1.x and work through. Please point me to any Contribution documents / rules for the repo.

Regards, Pratik

pratik-mahamuni avatar Aug 31 '22 18:08 pratik-mahamuni

killercup was the main force behind it. I'm just helping to maitnain

Sure, we'll take them. Looks like we don't have much besides a Code of Conduct. Maybe as we go, we'll have ideas of what should be documented :)

epage avatar Aug 31 '22 19:08 epage