0

I'm trying to learn Rust, but even with a very simple program i'm hitting a wall.

The app goal is to read a path from cmd line and list the directory content of that path. If I hardcode any path, it works without any error. If i use the path entered by the user, the error thread 'main' panicked at 'Reading directory failed: Os { code: 123, kind: InvalidFilename, message: "The filename, directory name, or volume label syntax is incorrect." }', src\main.rs:20:37.

My code:

use std::fs::read_dir;
use std::io;
use std::ffi::OsString;

fn main() {
    println!("Please input the path of the directory.");

    let mut buffer = String::new();

    io::stdin()
        .read_line(&mut buffer)
        .expect("Failed to read line");

    let mut osString : OsString = OsString::new();
    osString.push(&buffer[0..buffer.len()]);

    let paths = read_dir(&osString).expect("Reading directory failed");

Thanks in advance for your help.

infeo
  • 36
  • 1
  • As a first step: Add an debugging output after the user entered a path, show your input, and make sure the path exists. – the busybee Aug 11 '22 at 08:21
  • 2
    TLDR the duplicate: `read_line` includes the final newline in the returned string. Call `buffer = buffer.trim()` to get rid of it. – Jmb Aug 11 '22 at 08:41

0 Answers0