Rust \u5b66\u4e60\u7b14\u8bb0
\u8fd9\u662f\u4e00\u672c\u5173\u4e8e Rust \u7f16\u7a0b\u8bed\u8a00\u7684\u5b66\u4e60\u7b14\u8bb0\u3002
Rust \u662f\u4e00\u95e8\u6ce8\u91cd\u6027\u80fd\u3001\u53ef\u9760\u6027\u548c\u5b89\u5168\u6027\u7684\u7cfb\u7edf\u7ea7\u7f16\u7a0b\u8bed\u8a00\u3002
\u4e3a\u4ec0\u4e48\u5b66 Rust\uuff1f
- \u5185\u5b58\u5b89\u5168\uff0c\u65e0\u6570\u636e\u7ade\u4e89
- \u96f6\u6210\u672c\u62bd\u8c61
- \u9ad8\u6027\u80fd\uff0c\u65e0 GC
- \u4f18\u79c0\u7684\u5de5\u5177\u94fe (cargo)
graph TD
A[\u5b66\u4e60 Rust] --> B[\u6240\u6709\u6743\u7cfb\u7edf]
A --> C[\u7c7b\u578b\u7cfb\u7edf]
A --> D[\u5e76\u53d1\u7f16\u7a0b]
B --> E[\u7cbe\u901a Rust]
C --> E
D --> E
\u5feb\u901f\u5f00\u59cb
\u4f7f\u7528 Cargo \u521b\u5efa\u7b2c\u4e00\u4e2a Rust \u9879\u76ee\u3002
\u5b89\u88c5 Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
\u521b\u5efa\u9879\u76ee
cargo new hello_rust
cd hello_rust
cargo run
Hello World
fn main() { println!("Hello, Rust!"); }
\u4f7f\u7528 cargo clippy \u68c0\u67e5\u4ee3\u7801\u8d28\u91cf\uff0c\u4f7f\u7528 cargo fmt \u683c\u5f0f\u5316\u4ee3\u7801\u3002
\u6240\u6709\u6743\u7cfb\u7edf
Rust \u7684\u6838\u5fc3\u7279\u6027\u4e4b\u4e00\u5c31\u662f\u6240\u6709\u6743\u7cfb\u7edf\u3002
\u89c4\u5219
- Rust \u4e2d\u6bcf\u4e2a\u503c\u90fd\u6709\u4e00\u4e2a\u6240\u6709\u8005 (owner)
- \u540c\u4e00\u65f6\u95f4\u53ea\u80fd\u6709\u4e00\u4e2a\u6240\u6709\u8005
- \u5f53\u6240\u6709\u8005\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u503c\u88ab\u4e22\u5f03
fn main() { let s1 = String::from("hello"); let s2 = s1; // s1 \u7684\u6240\u6709\u6743\u8f6c\u79fb\u7ed9 s2 // println!("{}", s1); // \u7f16\u8bd1\u9519\u8bef\uff01s1 \u5df2\u4e0d\u53ef\u7528 println!("{}", s2); // OK }
\u6ce8\u610f\u533a\u5206\u79fb\u52a8\u8bed\u4e49\u548c\u590d\u5236\u8bed\u4e49\u3002\u5bf9\u4e8e\u5806\u4e0a\u6570\u636e\uff08\u5982 String\uff09\uff0c\u8d4b\u503c\u4f1a\u8f6c\u79fb\u6240\u6709\u6743\uff1b\u5bf9\u4e8e\u6808\u4e0a\u6570\u636e\uff08\u5982 i32\uff09\uff0c\u8d4b\u503c\u4f1a\u590d\u5236\u503c\u3002
\u501f\u7528\u4e0e\u5f15\u7528
fn calculate_length(s: &String) -> usize { s.len() } fn main() { let s1 = String::from("hello"); let len = calculate_length(&s1); println!("'{}' \u7684\u957f\u5ea6\u662f {}", s1, len); }