1

I am writing a method which would parse content of a file. For testing purposes, I would like to pass a &str to this method.

I was not able to find a way to do this, but there probably is some trait which does this. Maybe a different approach is required?

So far I have this:

fn read_buff<R: Read>(buffer: BufReader<R>) {}

This fails to compile:

let s = "str";
let mut reader = BufReader::new(s); // fails to compile.
read_buff(reader);

It would be best if I had access to the methods provided by the BufReader such as read_line.

Aleksander Fular
  • 803
  • 9
  • 18
  • 1
    The two snippets you've provided aren't really related in any way. Can you provide an [mcve] for us? – Simon Whitehead Jan 17 '17 at 00:13
  • 1
    The only specific addition for this question to the linked duplicate is [`str::as_bytes`](https://doc.rust-lang.org/std/primitive.str.html#method.as_bytes). – Shepmaster Jan 17 '17 at 00:17
  • @SimonWhitehead not really sure how snippets were unrelated, I just wanted to show that you can not create BufReader from &str. Anyway, thanks to Shepmaster, invoking the 'as_bytes()' does the trick. – Aleksander Fular Jan 17 '17 at 00:27
  • @AleksanderFular I was confused given your `read_buff` example, but the example failing to compile didn't use `read_buff` at all. Glad you have your answer though. – Simon Whitehead Jan 17 '17 at 00:29
  • @SimonWhitehead Yep, I guess I was missing the invocation to the read_buff method :). Edited. – Aleksander Fular Jan 17 '17 at 00:31

0 Answers0