How to declare a generic of mutable ref?

I'm creating a wrapper around a commandline app and after running the code in a simple main function I'm trying to declare a struct containing all the variables / members of interest. However I have some problems with the BufReader...

pub struct PreciseEngine {
    process: Child,
    model: String,
    chunk_size: usize,
    reader: BufReader<ChildStdout>

and a function to create the struct

pub fn get_runner() -> PreciseEngine {
    let mut child = Command::new("bash").stdin(Stdio::piped())

    let out = child.stdout.as_mut().unwrap();
    let mut cmd_out = BufReader::new(out);

    let p = PreciseEngine {
        process: child,
        model: "".to_string(),
        chunk_size: 2048,
        reader: cmd_out

This however leads to the error

rror[E0308]: mismatched types
  --> src/
51 |         reader: cmd_out
   |                 ^^^^^^^ expected struct `ChildStdout`, found `&mut ChildStdout`
   = note: expected struct `BufReader<ChildStdout>`
              found struct `BufReader<&mut ChildStdout>`

My question is how do I declare the reader member to be of type &mut ChildStdout?

I'm new at this so it's also is very possible that I'm doing something completely any help and pointers is appreciated :slight_smile:


  • Åke

Try this instead:

let out = child.stdout.take().unwrap();

Builds! Thanks

Does this move instead of borrowing? or something like that? (I'm like a child just mimicing the expressions I hear)

You can always read the documentation of the relevant method to find out what it does.

1 Like


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.