我的主人:
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);
}
我编译了它:
cargo build --release --target wasm32-unknown-emscripten
我创建了一个简单的HTML文件,我调用了JavaScript文件,我保存在这里:
target\wasm32-unknown-emscripten\release
我跑的时候
emrun --browser firefox --port 8080 tryrustwebasm.html
我的浏览器的控制台日志中有错误:
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
当我运行没有emrun
的程序时:
cargo run
程序运行正常。
我做错了什么?这是权限问题吗?我该如何解决?