如何从函数 return 当前工作目录?
how to return current working directory from function?
具有使用高级编程语言的经验。我读了 Rust 这本书,现在正努力生存并理解 Rust 中的“事物”是如何工作的。我希望有人解释一下到底是什么 - Ok(()) 以及如何处理它?我的目标是 return 来自函数的结果到输出的变量:
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/rcp ./file/ aba`
Ok(
"/home/tomand/rcp",
)
完整代码如下:
use std::fs;
use std::env;
use serde_json;
use regex::Regex;
use std::path::Path;
fn determinate_file_size(file: &str) -> u64 {
fs::metadata(file).unwrap().len()
}
fn determinate_is_it_file_or_dirctory(arg: &str) -> &str {
let file = "File";
let dir = "Directory";
let re = Regex::new(r"/").unwrap();
if re.is_match(arg) {
return dir;
}
return file;
}
fn collect_user_arguments() -> Vec<String> {
env::args().collect()
}
fn check_if_arguments_count_valid(args: &Vec<String>) -> bool {
if args.len() == 3 {
return true
}
help();
return false
}
fn get_current_working_dir() -> Result<T> {
env::current_dir()
}
fn help() {
println!("Examples:");
println!("rcp [srcfile] [destfile]");
println!("rcp [srcdir]/[srcfile] [destdir]/[destfile]");
}
fn main() {
let WORKING_DIR = get_current_working_dir();
let args: Vec<String> = collect_user_arguments();
if check_if_arguments_count_valid(&args) {
let arg1 = &args[1];
let arg2 = &args[2];
println!("{:#?}", determinate_is_it_file_or_dirctory(&arg1));
}
}
似乎编译器试图给我一些灵感,但最终我们最终沟通不畅:
error[E0107]: this enum takes 2 generic arguments but 1 generic argument was supplied
--> src/main.rs:42:33
|
42 | fn get_current_working_dir() -> Result<T> {
| ^^^^^^ - supplied 1 generic argument
| |
| expected 2 generic arguments
编辑:
我采用了这种方法:
fn get_current_working_dir() -> String {
let res = env::current_dir();
match res {
Ok(path) => path.into_os_string().into_string().unwrap(),
Err(_) => "FAILED".to_string()
}
}
似乎需要更多的练习才能理解 Result 类型以及如何管理它。
std::env::current_dir
returns a std::io::Result<Pathbuf>
,所以你需要在包装方法中使用那个类型:
fn get_current_working_dir() -> std::io::Result<PathBuf> {
env::current_dir()
}
其他挑剔的:
const
不是类型所以 let WORKING_DIR: const = get_current_working_dir();
是错误的,let WORKING_DIR = get_current_working_dir();
就足够了。
具有使用高级编程语言的经验。我读了 Rust 这本书,现在正努力生存并理解 Rust 中的“事物”是如何工作的。我希望有人解释一下到底是什么 - Ok(()) 以及如何处理它?我的目标是 return 来自函数的结果到输出的变量:
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Running `target/debug/rcp ./file/ aba`
Ok(
"/home/tomand/rcp",
)
完整代码如下:
use std::fs;
use std::env;
use serde_json;
use regex::Regex;
use std::path::Path;
fn determinate_file_size(file: &str) -> u64 {
fs::metadata(file).unwrap().len()
}
fn determinate_is_it_file_or_dirctory(arg: &str) -> &str {
let file = "File";
let dir = "Directory";
let re = Regex::new(r"/").unwrap();
if re.is_match(arg) {
return dir;
}
return file;
}
fn collect_user_arguments() -> Vec<String> {
env::args().collect()
}
fn check_if_arguments_count_valid(args: &Vec<String>) -> bool {
if args.len() == 3 {
return true
}
help();
return false
}
fn get_current_working_dir() -> Result<T> {
env::current_dir()
}
fn help() {
println!("Examples:");
println!("rcp [srcfile] [destfile]");
println!("rcp [srcdir]/[srcfile] [destdir]/[destfile]");
}
fn main() {
let WORKING_DIR = get_current_working_dir();
let args: Vec<String> = collect_user_arguments();
if check_if_arguments_count_valid(&args) {
let arg1 = &args[1];
let arg2 = &args[2];
println!("{:#?}", determinate_is_it_file_or_dirctory(&arg1));
}
}
似乎编译器试图给我一些灵感,但最终我们最终沟通不畅:
error[E0107]: this enum takes 2 generic arguments but 1 generic argument was supplied
--> src/main.rs:42:33
|
42 | fn get_current_working_dir() -> Result<T> {
| ^^^^^^ - supplied 1 generic argument
| |
| expected 2 generic arguments
编辑: 我采用了这种方法:
fn get_current_working_dir() -> String {
let res = env::current_dir();
match res {
Ok(path) => path.into_os_string().into_string().unwrap(),
Err(_) => "FAILED".to_string()
}
}
似乎需要更多的练习才能理解 Result 类型以及如何管理它。
std::env::current_dir
returns a std::io::Result<Pathbuf>
,所以你需要在包装方法中使用那个类型:
fn get_current_working_dir() -> std::io::Result<PathBuf> {
env::current_dir()
}
其他挑剔的:
const
不是类型所以 let WORKING_DIR: const = get_current_working_dir();
是错误的,let WORKING_DIR = get_current_working_dir();
就足够了。