摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/iter_result.html 示例 整个迭代失败 fn main() { let strings = vec!["tofu", " 阅读全文
posted @ 2021-12-24 15:54 jiangbo4444 阅读(71) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/iter_result.html 示例 包含错误值 fn main() { let strings = vec!["tofu", "9 阅读全文
posted @ 2021-12-24 15:53 jiangbo4444 阅读(58) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/wrap_error.html 示例 main.rs use std::error; use 阅读全文
posted @ 2021-12-24 15:51 jiangbo4444 阅读(84) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/reenter_question_mark.html 示例 main.rs use std: 阅读全文
posted @ 2021-12-24 15:50 jiangbo4444 阅读(817) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/boxing_errors.html 示例 main.rs use std::error; 阅读全文
posted @ 2021-12-24 15:49 jiangbo4444 阅读(356) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/define_error_type.html 示例 main.rs type Result< 阅读全文
posted @ 2021-12-24 15:48 jiangbo4444 阅读(109) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/option_result.html 示例 返回 Option use std 阅读全文
posted @ 2021-12-24 15:47 jiangbo4444 阅读(72) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/multiple_error_types.html 示例 main.rs fn double_first(vec: Vec<&str> 阅读全文
posted @ 2021-12-24 15:11 jiangbo4444 阅读(133) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/result/enter_question_mark.html 示例 如果想得到值而不产生恐慌,有一种简单的方式,那就是使用问号。 m 阅读全文
posted @ 2021-12-24 15:10 jiangbo4444 阅读(236) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/result/early_returns.html 示例 main.rs use std::num::ParseIntError; f 阅读全文
posted @ 2021-12-24 15:09 jiangbo4444 阅读(166) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/result/result_alias.html 示例 main.rs use std::num::ParseIntError; // 阅读全文
posted @ 2021-12-24 15:08 jiangbo4444 阅读(109) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/result/result_map.html 示例 Result 的 map 方法可以对值进行转换。 繁琐版本 use std::nu 阅读全文
posted @ 2021-12-24 15:07 jiangbo4444 阅读(322) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/result.html 示例 Result 和 Option 有点类似,不过它代表可能失败,而不是可能不存在。 恐慌 fn multi 阅读全文
posted @ 2021-12-24 15:06 jiangbo4444 阅读(245) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/option_unwrap/and_then.html 示例 main.rs #![allow(dead_code)] #[deriv 阅读全文
posted @ 2021-12-24 15:04 jiangbo4444 阅读(124) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/option_unwrap/map.html 示例 Option 有个 map 方法,可以进行值的映射,或者说转换。 main.rs 阅读全文
posted @ 2021-12-24 15:03 jiangbo4444 阅读(369) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/option_unwrap/question_mark.html 示例 使用问号进行展开,具有更好的可读性。 main.rs stru 阅读全文
posted @ 2021-12-24 15:02 jiangbo4444 阅读(243) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/option_unwrap.html 示例 除了使用恐慌,还可以使用可选(Option)来处理一些意外情况。 main.rs fn g 阅读全文
posted @ 2021-12-24 15:01 jiangbo4444 阅读(317) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/error/panic.html 示例 恐慌(panic)和其它语言中的异常和错误类似,一般针对不可处理的错误。如果没有经过处理,就会直接退出程序 阅读全文
posted @ 2021-12-24 14:59 jiangbo4444 阅读(64) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/macros/variadics.html 示例 main.rs macro_rules! calculate { (eval $e:expr) 阅读全文
posted @ 2021-12-24 14:57 jiangbo4444 阅读(492) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/macros/dsl.html 示例 DSL 表示 Domain Specific Language,领域特定语言。 main.rs macro_ 阅读全文
posted @ 2021-12-24 14:55 jiangbo4444 阅读(169) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/macros/dry.html 示例 DRY 表示 Don't Repeat Yourself,不要重复你自己,意思是尽量不要写重复的代码。 ma 阅读全文
posted @ 2021-12-24 14:53 jiangbo4444 阅读(159) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/macros/repeat.html 示例 使用星号表示零到多次,使用加号表示一到多次。 main.rs macro_rules! find_mi 阅读全文
posted @ 2021-12-24 14:52 jiangbo4444 阅读(153) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/macros/overload.html 示例 main.rs macro_rules! test { ($left:expr ;and $rig 阅读全文
posted @ 2021-12-24 14:51 jiangbo4444 阅读(97) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/macros/designators.html 示例 以下是可以使用的标志符: block expr 表达式 ident 变量名或函数名 item 阅读全文
posted @ 2021-12-24 14:50 jiangbo4444 阅读(211) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/macros.html 示例 宏和函数有点像,不过是以叹号结尾的,经常使用的 println! 就是一个宏。宏不是函数调用,而是直接展开到代码中。 阅读全文
posted @ 2021-12-24 14:48 jiangbo4444 阅读(219) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/trait/disambiguating.html 示例 如果有两个同名的方法,这时调用的时候,需要使用指定调用的类型。 静态返回 trait U 阅读全文
posted @ 2021-12-24 14:45 jiangbo4444 阅读(81) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/trait/supertraits.html 示例 类似其它语言中的接口继承。 main.rs trait Person { fn name(&s 阅读全文
posted @ 2021-12-24 14:44 jiangbo4444 阅读(148) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/trait/clone.html 示例 main.rs #[derive(Debug, Clone, Copy)] struct Unit; #[ 阅读全文
posted @ 2021-12-24 14:42 jiangbo4444 阅读(115) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/trait/impl_trait.html 示例 静态返回 fn combine_vecs(v: Vec<i32>, u: Vec<i32>) - 阅读全文
posted @ 2021-12-24 11:27 jiangbo4444 阅读(88) 评论(0) 推荐(0)
摘要: 环境 Rust 1.56.1 VSCode 1.61.2 概念 参考:https://doc.rust-lang.org/rust-by-example/trait/iter.html 示例 区间手动迭代 fn main() { // `0..3` is an `Iterator` that gen 阅读全文
posted @ 2021-12-24 11:26 jiangbo4444 阅读(68) 评论(0) 推荐(0)