using this command cargo run --release > image.ppm to output to .ppm format it is encoded in utf-16 which prevent the file from opening, so what should I do to make it output with utf-8 encoding.
Just a blind guess but is your terminal configured to use UTF-16? Maybe Rust defaults to the terminal's settings when writing strings to stdout, not sure.
Instead of writing a string to stdout using print! (and hoping that the right encoding is chosen) you could encode it to UTF-8 yourself (e.g. using the as_bytes method of String which is guaranteed to be UTF-8 encoded) and then use std::io::Stdout::write:
use std::io::{self, Write};
fn main() {
let mut stdout = io::stdout();
stdout.write(format!("Hello {}!", "there").as_bytes()).unwrap();
}
If you're on Windows, and you're using PowerShell, you need to stop using PowerShell. This has nothing to do with Rust.
PowerShell is hard-coded to forcibly translate all text that goes through it into UTF-16, and as far as I can tell, there is no way to change this or turn it off.
I mean, that or change your program to write to a file instead of stdout. That'd work, too.