Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise solutions #124

Open
dycw opened this issue Feb 20, 2022 · 3 comments
Open

Exercise solutions #124

dycw opened this issue Feb 20, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@dycw
Copy link

dycw commented Feb 20, 2022

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?

@epage
Copy link
Contributor

epage commented Feb 21, 2022

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

@epage epage added the enhancement New feature or request label Feb 21, 2022
@pratik-mahamuni
Copy link

pratik-mahamuni commented Aug 31, 2022

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

@epage
Copy link
Contributor

epage commented Aug 31, 2022

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants