Rust 遇上 C/C++ (一):數組操作

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust 第一個穩定版本於 2015 年發佈,與上世紀 80 年代左右誕生的 C/C++ 相比,可謂是後起之秀。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同樣作爲系統級編程語言,Rust 的設計準則爲安全、速度和併發。當然,C/C++ 可能對此表示不服!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼當 Rust 遇上 C/C++ 會在哪些方面進行討論呢?我們先從"},{"type":"text","marks":[{"type":"strong"}],"text":"數組操作"},{"type":"text","text":"談起。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"環境:Cent OS 8,gcc,g++,cargo。"}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"C"}]},{"type":"codeblock","attrs":{"lang":"c"},"content":[{"type":"text","text":"#include \n\nint main() {\n int a[] = {1, 2, 3, 4, 5};\n int index = 10;\n\n int element = a[index];\n\n printf(\"The value of element is: %d\\n\", element);\n\n return 0;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"$ gcc main.c\n$ ./a.out\nThe value of element is: -721516429"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編譯沒有產生任何錯誤,程序正常運行,“成功”訪問了無效的內存 a[10]。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"C++"}]},{"type":"codeblock","attrs":{"lang":"cpp"},"content":[{"type":"text","text":"#include \n\nusing namespace std;\n\nint main() {\n int a[] = {1, 2, 3, 4, 5};\n int index = 10;\n\n int element = a[index];\n\n cout << \"The value of element is: \" << element << endl;\n\n return 0;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"$ g++ main.cc\n$ ./a.out\nThe value of element is: 1914456179"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同樣,編譯沒有產生任何錯誤,程序正常運行並訪問了無效的內存 a[10]。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Rust"}]},{"type":"codeblock","attrs":{"lang":"rust"},"content":[{"type":"text","text":"fn main() {\n let a = [1, 2, 3, 4, 5];\n let index = 10;\n\n let element = a[index];\n\n println!(\"The value of element is: {}\", element);\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"$ cargo run\n Compiling varibles v0.1.0 (/home/ervin/rust)\n Finished dev [unoptimized + debuginfo] target(s) in 0.19s\n Running `target/debug/varibles`\nthread 'main' panicked at 'index out of bounds: the len is 5 but the index is 10', src/main.rs:5:19\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"編譯沒有產生任何錯誤,但程序導致運行時錯誤並沒有成功退出。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當我們嘗試使用索引訪問一個元素時,Rust 將檢查您指定的索引是否小於數組長度。如果索引大於或等於數組長度,Rust 將陷入 Panic。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當提供不正確的索引時,C/C++ 可以訪問無效的內存。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust通過立即退出而不是允許內存訪問,從而避免這種錯誤。大家是否覺得這樣更安全呢?"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust 由於提供了額外安全保證,可能會降低一些性能。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"哪有什麼歲月靜好,都是有人替你負重前行!"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然,如果 C/C++ 的使用者足夠細心,自己能提供安全保證的話,是同樣可以避免錯誤的。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章