1

I am writing a rust terminal music player and i got stuck when i tried to list all files inside a dir which the user entered

main.rs

use std::path::*;
use std::io::*;
fn main() {
    let mut file_path = String::new();

    println!("Enter music file path");
    stdin().read_line(&mut file_path).unwrap();

    // rusty_music::play_audio(&file_path.trim());
    rusty_music::display_all_files(&Path::new(&file_path));

}

lib.rs

use rodio::{source::Source, Decoder, OutputStream};
use std::fs::*;
use std::io::BufReader;
use std::path::Path;
pub fn display_all_files(path: &Path) {
    println!("{p}", p = path.display());
    for file in read_dir(path).unwrap() {
        println!("{:?}", file);
    }
}



I was expecting to list all files and folder inside the dir Any help in fixing my code would be great

ayush mishra
  • 9
  • 1
  • 5
  • You should also say what it does instead of what you expected. – Sebastian Redl Mar 01 '23 at 09:22
  • Its give an error when i enter yeet (the dir name) and yeet is located in the program's root and iin target/release dir also ```thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/lib.rs:21:59 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` @SebastianRedl – ayush mishra Mar 01 '23 at 09:28
  • That error occurs because your `path` includes a trailing newline. – Jmb Mar 01 '23 at 11:25

0 Answers0