Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pipe rust output

What I’m trying to do

I’m trying to apply a substitution with sed on an output of the rust compiler.

Small example

Do cargo new test.

src/main.rs

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fn main() {
    println("Println is a macro!!");
}

Now do cargo run | sed -n -e 's/help/Help/'.

Expected output

error[E0423]: expected function, found macro `println`
 --> src/main.rs:2:5
  |
2 |     println("Println is a macro!!");
  |     ^^^^^^^ not a function
  |
Help: use `!` to invoke the macro
  |
2 |     println!("Println is a macro!!");
  |            +

For more information about this error, try `rustc --explain E0423`.
error: could not compile `rust_tmp` due to previous error

What I get

error[E0423]: expected function, found macro `println`
 --> src/main.rs:2:5
  |
2 |     println("Println is a macro!!");
  |     ^^^^^^^ not a function
  |
help: use `!` to invoke the macro
  |
2 |     println!("Println is a macro!!");
  |            +

For more information about this error, try `rustc --explain E0423`.
error: could not compile `rust_tmp` due to previous error

(nothing has changed)

What else I tried

I also tried:

  • cargo run 2>&1 sed -n -e 's/help/Help/'

>Solution :

Try piping stderr instead of stdout to sed: cargo run 3>&1 1>&2 2>&3 | sed -n -e 's/help/Help/'.
You can also pipe both stderr & stdout to sed: cargo run |& sed -n -e 's/help/Help/'.

Have a look at the bash manual chapter on redirections.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading