I wrote this. improvements and suggestions?

I wrote this and it works. I accept improvements and suggestions:

use std::process::{Command, Stdio}; 
use std::{fs, io};
use std::path::Path;
// ==================== CONSTANTS
const _DESTINY: &str = "/home/pichibw/01_HANDBRAKE/";

// ==================== FUNCTIONS
fn ffmpeg(aud: &String, sub: &String, src: &String, dst: &String) {    
    let salida = Command::new("HandBrakeCLI")
    .arg(&aud) // audio tracks (ej. 1 or 1,2)
    .arg(&sub) // subtitle tracks (ej. 1 or 1,2)
    .arg(&src) // origin
    .arg(&dst) //destiny    
    .expect("Failed to execute command");

fn main() {
    Default Destiny is: {}
    ", &DESTINY);
    // var to store result for FFMPEG
    let mut aud_track = String::new();
    let mut sub_track = String::new();
    // reading audio track
    println!("give me audio track (Ej. 1 or 1,2):");
    let stdin_audio = io::stdin();
    stdin_audio.read_line(&mut aud_track);
    println!("Pista de audio: {} ", &aud_track);
    // reading subtitle track
    println!("give me subtitle track (Ej. 1 or 1,2):");
    let stdin_subtitle = io::stdin();
    stdin_subtitle.read_line(&mut sub_track);
    println!("Pista de subtitulo: {} ", &sub_track);
    // reading video folder
    println!("Give me video folder:");
    let mut video_folder = String::new();
    let stdin = io::stdin();
    stdin.read_line(&mut video_folder);
    println!("Video folder: {} ", video_folder);
    // read files in dir    
    for file in fs::read_dir(video_folder.replace("\n", "")).unwrap() {
        let video_src = &file.unwrap().path().display().to_string();
        let video_src_split = video_src.split("/");
        let mut vector = Vec::new();
        for a in video_src_split {                        
        let mut video_name = vector[vector.len()-1];
        let mut destiny = format!("/home/pichibw/01_HANDBRAKE/{}", &video_name);
        println!("Video name: {} ", &video_name);
        println!("Video destiny: {} ", &destiny);
        ffmpeg(&aud_track, &sub_track, &video_src, &destiny);

thank you for your time.

  1. Run clippy and fix all its lints: Rust Playground
  2. You may want to use Command::args to pass all the arguments at once.
  3. Command::output implies that stdout is piped so no need to set it manually.
  4. You should extract the logic of reading a line after giving a prompt into a function.
    • This function should print the prompt to stderr, not stdout.
  5. Replacing newlines with the empty string looks like it's meant to trim the newline. Your prompting function should probably trim the newline automatically, making this unnecessary. If you do need it, though, use str::trim instead.
  6. The complex manipulation to get video_name is just Path::file_name.
  7. You shouldn't use string formatting to make the output path. Use Path::join instead.
  8. Since you'll be using Paths instead of Strings now, some of the arguments to the ffmpeg function should probably become Paths or OsStrs. This may interfere with the Command::args invokation, but you can split it into multiple args invokations to fix this if you like.
  9. Don't do the //============== CONSTANTS thing.
  10. Newlines in the print string are kind of weird. I would prefer splitting it into two prints. (By the way, that should go to stderr too.)
  11. As a somewhat new feature, you can put simple format arguments directly in the format string: eprintln!("Default destiny is {DESTINY:?}.");. Same with the other prints in the code.
  12. You may want to use an argument parser like argh to get the inputs. Just an idea.

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.