From 9a1f8f7b42f9bfa7a1bc619568bffe08d16c16ac Mon Sep 17 00:00:00 2001 From: sunface Date: Thu, 17 Feb 2022 19:22:48 +0800 Subject: [PATCH] update toc and best-practice --- .../writing-material}/good-sourcecode.md | 0 contents/SUMMARY.md | 14 +++----- contents/confonding/string.md | 4 ++- contents/practice/best-pratice.md | 35 +++++++++++++++++++ contents/practice/coding-tips.md | 25 ------------- contents/practice/intro.md | 1 + contents/practice/{style-guide => }/naming.md | 0 contents/practice/observability.md | 1 + contents/practice/style-guide/clippy.md | 3 -- contents/practice/style-guide/code.md | 4 --- contents/practice/style-guide/intro.md | 1 - contents/practice/style-guide/mark.md | 4 --- 12 files changed, 44 insertions(+), 48 deletions(-) rename {contents/practice => assets/writing-material}/good-sourcecode.md (100%) delete mode 100644 contents/practice/coding-tips.md rename contents/practice/{style-guide => }/naming.md (100%) create mode 100644 contents/practice/observability.md delete mode 100644 contents/practice/style-guide/clippy.md delete mode 100644 contents/practice/style-guide/code.md delete mode 100644 contents/practice/style-guide/intro.md delete mode 100644 contents/practice/style-guide/mark.md diff --git a/contents/practice/good-sourcecode.md b/assets/writing-material/good-sourcecode.md similarity index 100% rename from contents/practice/good-sourcecode.md rename to assets/writing-material/good-sourcecode.md diff --git a/contents/SUMMARY.md b/contents/SUMMARY.md index 4f426c35..85d834fe 100644 --- a/contents/SUMMARY.md +++ b/contents/SUMMARY.md @@ -130,7 +130,6 @@ - [原生指针、引用和智能指针 todo](confonding/pointer.md) - [作用域、生命周期和 NLL todo](confonding/lifetime.md) - [move、Copy和Clone todo](confonding/move-copy.md) - - [写时拷贝Cow todo](confonding/cow.md) - [对抗编译检查 doing](fight-with-compiler/intro.md) - [幽灵数据(todo)](fight-with-compiler/phantom-data.md) @@ -159,15 +158,10 @@ - [Rust最佳实践 doing](practice/intro.md) - [日常开发三方库精选](practice/third-party-libs.md) - - [一些写代码的技巧 todo](practice/coding-tips.md) - - [最佳实践 todo](practice/best-pratice.md) - - [值得学习的源代码 todo](practice/good-sourcecode.md) - - [代码规范 doing](practice/style-guide/intro.md) - - [命名规范](practice/style-guide/naming.md) - - [代码风格(todo)](practice/style-guide/code.md) - - [代码标记 todo](practice/style-guide/mark.md) - - [Clippy todo](practice/style-guide/clippy.md) - - [日志和监控 todo](practice/logs.md) + - [代码开发实践 todo](practice/best-pratice.md) + - [命名规范](practice/naming.md) + - [日志 todo](practice/logs.md) + - [可观测性监控 todo](practice/observability.md) - [如何实现一个链表 todo]() diff --git a/contents/confonding/string.md b/contents/confonding/string.md index 94dd2bfb..09b0578d 100644 --- a/contents/confonding/string.md +++ b/contents/confonding/string.md @@ -23,13 +23,15 @@ error[E0277]: the size for values of type `str` cannot be known at compilation t | ^^^^^^ doesn't have a size known at compile-time ``` +如果追求更深层的原因,我们可以总结如下:**所有的切片都是动态类型,它们都无法直接被使用,而 `str` 就是字符串切片,`[u8]` 是数组切片。** + 同时还是 String 和 &str 的底层数据类型。 由于 str 是动态 `str` 类型是硬编码进可执行文件,也无法被修改,但是 `String` 则是一个可增长、可改变且具有所有权的 UTF8 编码字符串,**当 Rust 用户提到字符串时,往往指的就是 `String` 类型和 `&str` 字符串切片类型,这两个类型都是 UTF8 编码**。 除了 `String` 类型的字符串,Rust 的标准库还提供了其他类型的字符串,例如 `OsString`, `OsStr`, `CsString` 和` CsStr` 等,注意到这些名字都以 `String` 或者 `Str` 结尾了吗?它们分别对应的是具有所有权和被借用的变量。 -An str defines a slice of a block of data in memory which are meant to be interpreted as a sequence of characters and that’s all. It is uncertain where it is stored and how long that slice would be. This is why we can not use this type in a plain way in the time of writing. (Rust 1.58) + https://pic1.zhimg.com/80/v2-177bce575bfaf289ae12d677689a26f4_1440w.png https://pic2.zhimg.com/80/v2-697ad53cb502ccec4b2e98c40975344f_1440w.png diff --git a/contents/practice/best-pratice.md b/contents/practice/best-pratice.md index 481b902c..ca44b8d1 100644 --- a/contents/practice/best-pratice.md +++ b/contents/practice/best-pratice.md @@ -18,3 +18,38 @@ https://github.com/tkellogg/dura ## code cover https://docs.codecov.com/docs + +## clippy +https://www.reddit.com/r/rust/comments/s62xu0/inspect_enum_variant_size_differences/ + +## todo +unimplemented!() todo!() + +## 如何获知变量类型或者函数的返回类型 + +有几种常用的方式: + - 第一种是查询标准库或者三方库文档,搜索`File`,然后找到它的`open`方法,但是此处更推荐第二种方法: + - 在[Rust IDE]章节,我们推荐了`VSCode` IED和`rust-analyze`插件,如果你成功安装的话,那么就可以在`VScode`中很方便的通过代码跳转的方式查看代码,同时`rust-analyze`插件还会对代码中的类型进行标注,非常方便好用! + - 你还可以尝试故意标记一个错误的类型,然后让编译器告诉你: + +```rust +let f: u32 = File::open("hello.txt"); +``` +错误提示如下: + +```console +error[E0308]: mismatched types + --> src/main.rs:4:18 + | +4 | let f: u32 = File::open("hello.txt"); + | ^^^^^^^^^^^^^^^^^^^^^^^ expected u32, found enum +`std::result::Result` + | + = note: expected type `u32` + found type `std::result::Result` +``` + +# 代码风格(todo) + + +https://www.reddit.com/r/rust/comments/rlsatb/generically_working_with_futuressinkstream/ \ No newline at end of file diff --git a/contents/practice/coding-tips.md b/contents/practice/coding-tips.md deleted file mode 100644 index 252069a0..00000000 --- a/contents/practice/coding-tips.md +++ /dev/null @@ -1,25 +0,0 @@ -# 一些写代码的技巧 - -## 如何获知变量类型或者函数的返回类型 - -有几种常用的方式: - - 第一种是查询标准库或者三方库文档,搜索`File`,然后找到它的`open`方法,但是此处更推荐第二种方法: - - 在[Rust IDE]章节,我们推荐了`VSCode` IED和`rust-analyze`插件,如果你成功安装的话,那么就可以在`VScode`中很方便的通过代码跳转的方式查看代码,同时`rust-analyze`插件还会对代码中的类型进行标注,非常方便好用! - - 你还可以尝试故意标记一个错误的类型,然后让编译器告诉你: - -```rust -let f: u32 = File::open("hello.txt"); -``` -错误提示如下: - -```console -error[E0308]: mismatched types - --> src/main.rs:4:18 - | -4 | let f: u32 = File::open("hello.txt"); - | ^^^^^^^^^^^^^^^^^^^^^^^ expected u32, found enum -`std::result::Result` - | - = note: expected type `u32` - found type `std::result::Result` -``` \ No newline at end of file diff --git a/contents/practice/intro.md b/contents/practice/intro.md index 6c150438..7f89d0ee 100644 --- a/contents/practice/intro.md +++ b/contents/practice/intro.md @@ -1 +1,2 @@ # Rust最佳实践 +对于生产级项目而言,运行稳定性和可维护性是非常重要的,本章就一起来看看 Rust 项目有哪些最佳实践准则。 \ No newline at end of file diff --git a/contents/practice/style-guide/naming.md b/contents/practice/naming.md similarity index 100% rename from contents/practice/style-guide/naming.md rename to contents/practice/naming.md diff --git a/contents/practice/observability.md b/contents/practice/observability.md new file mode 100644 index 00000000..3b225569 --- /dev/null +++ b/contents/practice/observability.md @@ -0,0 +1 @@ +# 可观测性监控 todo diff --git a/contents/practice/style-guide/clippy.md b/contents/practice/style-guide/clippy.md deleted file mode 100644 index 9fcd99cb..00000000 --- a/contents/practice/style-guide/clippy.md +++ /dev/null @@ -1,3 +0,0 @@ -# Clippy - -https://www.reddit.com/r/rust/comments/s62xu0/inspect_enum_variant_size_differences/ \ No newline at end of file diff --git a/contents/practice/style-guide/code.md b/contents/practice/style-guide/code.md deleted file mode 100644 index 315170fd..00000000 --- a/contents/practice/style-guide/code.md +++ /dev/null @@ -1,4 +0,0 @@ -# 代码风格(todo) - - -https://www.reddit.com/r/rust/comments/rlsatb/generically_working_with_futuressinkstream/ \ No newline at end of file diff --git a/contents/practice/style-guide/intro.md b/contents/practice/style-guide/intro.md deleted file mode 100644 index 07d8088f..00000000 --- a/contents/practice/style-guide/intro.md +++ /dev/null @@ -1 +0,0 @@ -# Rust代码风格 \ No newline at end of file diff --git a/contents/practice/style-guide/mark.md b/contents/practice/style-guide/mark.md deleted file mode 100644 index 64cbea29..00000000 --- a/contents/practice/style-guide/mark.md +++ /dev/null @@ -1,4 +0,0 @@ -# 代码标记 - - -unimplemented!() todo!() \ No newline at end of file