My main.rs:
use std::fs::File;
use std::io::prelude::*;
fn read_file(file_name: &str, file_path: &str) -> Vec<String> {
let file_open_error = format!("File {} by path {} not found!", file_name, file_path);
let file_read_error = format!(
"Can't read the file {}\\{}, the error:{}",
file_path, file_name
);
let mut file = File::open(format!("{}\\{}", file_path, file_name)).expect(&*file_open_error);
let mut content = String::new();
file.read_to_string(&mut content).expect(&*file_read_error);
content
.split_whitespace()
.map(|s| s.to_string())
.collect::<Vec<String>>()
}
fn get_random_value<'a>(vec: &'a Vec<String>) -> &'a String {
let mut rng = rand::thread_rng();
rng.choose(&vec).unwrap()
}
fn main() {
let names_file_name = "names.txt";
let adjectives_file_name = "adjectives.txt";
let file_path = "D:\\nameadj";
let names = read_file(names_file_name, file_path);
let adjectives = read_file(adjectives_file_name, file_path);
let name = get_random_value(&names);
let adjective = get_random_value(&adjectives);
println!("name={};adjective={}", name, adjective);
}
I've compiled it:
cargo build --release --target wasm32-unknown-emscripten
I've created a simple HTML file where I call the JavaScript file, which I saved here:
target\wasm32-unknown-emscripten\release
When I run it
emrun --browser firefox --port 8080 tryrustwebasm.html
There is an error in the console log of my browser:
thread 'main' panicked at 'File names.txt by path D:\nameadj not found!: Error { repr: Os { code: 2, message: "No such file or directory" } }', /checkout/src/libcore/result.rs:906:4
When I run the program without emrun
:
cargo run
the program works fine.
What am I doing wrong? Is this a permissions problem? How do I fix it?