diff --git a/content/_index.md b/content/_index.md index dc68fb4c..a11c6730 100644 --- a/content/_index.md +++ b/content/_index.md @@ -259,3 +259,4 @@ - [Cargo 管理工具](rust/02-cargo.md) - [VSCode 调试](rust/03-vscode-debugging.md) - [Rust WASM 编程](rust/04-wasm-programming.md) +- [String 还是 &str](rust/string-&str.md) diff --git a/content/rust/04-wasm-programming.md b/content/rust/04-wasm-programming.md index 3bf05e99..f4ac4573 100644 --- a/content/rust/04-wasm-programming.md +++ b/content/rust/04-wasm-programming.md @@ -63,3 +63,5 @@ wasm-pack build --scope [npm-username] --- [« VSCode 调试](03-vscode-debugging.md) + +[» String 还是 &str](string-&str.md) diff --git a/content/rust/_index.md b/content/rust/_index.md index 8499deb6..ed0e680e 100644 --- a/content/rust/_index.md +++ b/content/rust/_index.md @@ -9,3 +9,5 @@ [VSCode 调试](03-vscode-debugging.md) [Rust WASM 编程](04-wasm-programming.md) + +[String 还是 &str](string-&str.md) diff --git a/content/rust/string-&str.md b/content/rust/string-&str.md new file mode 100644 index 00000000..e11e6595 --- /dev/null +++ b/content/rust/string-&str.md @@ -0,0 +1,14 @@ +[🏠 首页](../_index.md) / [Rust 编程](_index.md) / String 还是 &str + +# String 还是 &str + +好用的规则: + +1. 始终在结构体中使用 `String`; +2. 函数入参使用 `&str`,函数出参使用 `String`; +3. 如果函数出参来源于入参,并且函数中没有改变入参,那么出参使用 `&str`。 + +遵循以上的规则,大部分场景你将很好的避开 `String &str` 使用困难,即使有问题,编译器会告知你问题所在。 + +--- +[« Rust WASM 编程](04-wasm-programming.md)