basic-practice/refactoring #1152
Replies: 23 comments 18 replies
-
喜(令)闻(人)乐(讨)见(厌) |
Beta Was this translation helpful? Give feedback.
-
"像很多国内的大学教材一样,只要列出定理和解题方法,然后留下足够的习题,就万事大吉了",hh |
Beta Was this translation helpful? Give feedback.
-
rust 1.67.1 // 对 build 返回的 这里提示: |
Beta Was this translation helpful? Give feedback.
-
用 |
Beta Was this translation helpful? Give feedback.
-
重构后的代码就是优雅 |
Beta Was this translation helpful? Give feedback.
-
impl Config {
fn build(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 3 {
return Err("not enough arguments");
}
let query = args[1].clone();
let file_path = args[2].clone();
Ok(Config { query, file_path })
}
} 这里的 |
Beta Was this translation helpful? Give feedback.
-
main.rs引用lib.rs里的东西,可以通过ns::fn_name实现,如果引用其他文件里的东西,需要使用 include! |
Beta Was this translation helpful? Give feedback.
-
异常处理好晕,没有try.catch好理解. |
Beta Was this translation helpful? Give feedback.
-
太棒了,这一节把之前所学的东西基本上都串起来了。之前一直没弄懂Result类型的作用,现在联想到枚举类型一下子思路全畅通了😊 |
Beta Was this translation helpful? Give feedback.
-
问个问题,println!("In file {}\n", &config.file_path); 这个宏结束后应该会释放资源,config.file_path的所有权是转移到宏上去的,然后这个变量应该就不能用了。但是可以看到run(config);中又调用了它,并且String也没有实现copy trait,为什么还能正常运行啊 |
Beta Was this translation helpful? Give feedback.
-
把run 函式放進impl裡面是否有可能會更優雅一些?
也可以實現鏈式調用
這兩種哪一種會是比較常規的做法呀@@"? |
Beta Was this translation helpful? Give feedback.
-
写测试是喜(令)闻(人)乐(讨)见(厌)的,但是看着 coverage 慢慢涨起来还是蛮快乐的。 |
Beta Was this translation helpful? Give feedback.
-
fn main() -> Result<(), Box<dyn Error>>{
let args: Vec<String> = env::args().collect();
let config= Config::build(&args)?;
let _ = minigrep::run(config)?;
Ok(())
} |
Beta Was this translation helpful? Give feedback.
-
"处理返回的 Result" 卡住我半天。。。 这提示看不出来跟错误的use有任何关系。。。 |
Beta Was this translation helpful? Give feedback.
-
心累😭😭😭😭😭😭 |
Beta Was this translation helpful? Give feedback.
-
深入浅出,讲的好 |
Beta Was this translation helpful? Give feedback.
-
// in main.rs
fn main() {
let args: Vec<String> = env::args().collect();
let (query, file_path) = parse_config(&args);
// --省略--
}
fn parse_config(args: &[String]) -> (&str, &str) {
let query = &args[1];
let file_path = &args[2];
(query, file_path)
} 为啥函数调用 |
Beta Was this translation helpful? Give feedback.
-
&[String]好像是第一次见到,是&Vec的语法糖吗,还是说是一个可以隐式转换的其他类型? |
Beta Was this translation helpful? Give feedback.
-
basic-practice/refactoring
https://course.rs/basic-practice/refactoring.html
Beta Was this translation helpful? Give feedback.
All reactions