0

I am trying to create file using keyboard input, but the created file's name has unnecessary symbols.

use std::io;
use std::fs::File;

fn main() {
    let mut filesname = String::new();
    io::stdin().read_line(&mut filesname)
        .ok()
        .expect("cant read string");
    filecreate(&filesname);
}

fn filecreate(path: &str) {
    let f = File::create(path);
    println!("Ok, file {} was created.", path);
}

Using this code I wrote "foo.txt", but my file manager shows this file as "'foo.txt'$'\n'". I can't figure out what I have to do with the user's input.

2
  • 4
    read_line() also reads the newline; see this question for the same issue. Commented Sep 28, 2016 at 7:36
  • The only extra symbol is \n which is printed by ls as $'\n'. In bash, newlines can be represented inside single quotes if the single quotes are preceded by $, and strings can be concatenated by simply placing them beside each other. That is, $'foo\n' == 'foo'$'\n' == 'fo'$'o\n' == 'f''o''o'$'\n' == 'f'oo$'\n'. So just trim the final newline off! Commented Sep 28, 2016 at 10:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.