1

I'm going through the command line applications in rust book and trying to do some of the exercises that are there.

Problem: I would like to read the content of a file in memory using BufReader instead of read_to_string(), but I'm getting confused on how to use it.

Error message: no method named read_lines found for type std::io::BufReader<&std::path::PathBuf> in the current scope, which makes sense. Where can I find the methods for BufReader

Tangent question: is BufReader a class or just an object that was constructed with the new keyword like in javascript?

use std::env;
use std::io::BufReader;
use structopt::StructOpt;

#[derive(StructOpt)]
struct Cli {
    /// the pattern to look for
    pattern: String,
    /// the path to the file to read
    #[structopt(parse(from_os_str))]
    path: std::path::PathBuf,
}

fn help() {
    println!(
        "usage:
    this is just a dummy help
    message to see how functions work in rust"
    );
}

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

    // [NOTE]: This works

    // [ ] Ex:2 Use BufReader instead of read_to_string()
    let content = std::fs::read_to_string(&args.path).expect("Could not read file");

    for line in content.lines() {
        if line.contains(&args.pattern) {
            println!("{:#?}", line);
        }
    }

    // [NOTE]: What I am trying to do:

    let mut reader = BufReader::new(&args.path);
    // .expect("could not read file");
    for line in reader.read_lines() {
        if line.contains(&args.pattern) {
            println!("{:#?}", line);
        }
    }
}
intercoder
  • 2,171
  • 7
  • 23
  • 34
  • 2
    Please describe your actual problem. What does not work? Do you get an error? If so, please [edit] your question and post it. – hellow Jul 16 '19 at 08:34
  • 1
    To "answer" your tangent question: [`BufReader`](https://doc.rust-lang.org/std/io/struct.BufReader.html) is a struct (that's how we call it in Rust, not class). `new` is just a function associated with the struct so you can call `BufReader::new`. Rust doesn't have the `new` operator like Javascript, e.g. **no** `new BufReader`. – hellow Jul 16 '19 at 08:35
  • 1
    "Where can I find the methods for BufReader" => https://doc.rust-lang.org/std/io/struct.BufReader.html – Stargateur Jul 16 '19 at 09:09
  • @hellow I think it's a little disingenuous to suggest `struct` is 'how we call' a class (c.f. https://doc.rust-lang.org/book/ch05-00-structs.html) – doctor_n_ Jul 16 '19 at 21:02
  • 1
    @doctor_n_ I don't get what you want to tell me? In Rust there is no such keyword class nor such a concept. It uses structs. – hellow Jul 17 '19 at 07:06
  • @hellow yeah exactly, saying 'how we call' implies they're the same thing with different names. As you've just said, they're not the same thing. – doctor_n_ Jul 17 '19 at 14:04
  • @doctor_n_ `class` and `struct` fulfill the same purpose, namely to group variables into one and having associated functions. That's why I said: "what you call class, is what "we" call struct". It's nothing more than saying, in Java you have Classes, in Rust we have structs. It is (almost) the same thing. If you disaggree, please show me a counter example. – hellow Jul 17 '19 at 14:27
  • @hellow a `class` introduces the notion of inheritance and (in most object-oriented languages) dynamic method dispatch. Neither of these are supported by `struct`s (without careful use of `trait`s and explicit composition), so I think it's wrong to conflate the notion of a `struct` and a `class`. – doctor_n_ Jul 18 '19 at 12:57

0 Answers0