ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
ข้อมูลเบื้องต้นเกี่ยวกับ Rust สำหรับ Android
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แพลตฟอร์ม Android รองรับการพัฒนาคอมโพเนนต์ระบบปฏิบัติการแบบเนทีฟใน Rust ซึ่งเป็นภาษาการเขียนโปรแกรมระบบสมัยใหม่ที่รับประกันความปลอดภัยของหน่วยความจำพร้อมประสิทธิภาพเทียบเท่า C/C++ โดย Rust ใช้การตรวจสอบแบบรวมเวลาคอมไพล์ที่บังคับใช้อายุการใช้งานและกรรมสิทธิ์ของออบเจ็กต์ รวมถึงการตรวจสอบรันไทม์ที่ตรวจสอบการเข้าถึงหน่วยความจำที่ถูกต้อง จึงไม่จำเป็นต้องใช้โปรแกรมเก็บขยะ
Rust มีฟีเจอร์ภาษาสมัยใหม่มากมายที่ช่วยให้นักพัฒนาซอฟต์แวร์ทำงานได้อย่างมีประสิทธิภาพและมั่นใจมากขึ้นในโค้ดของตน
- การเขียนโปรแกรมแบบขนานอย่างปลอดภัย - ความสามารถในการเขียนโค้ดที่มีประสิทธิภาพและปลอดภัยสำหรับเธรดของ Rust ทำให้ Rust เกิดสโลแกนว่าการเขียนโปรแกรมแบบขนานที่ไร้กังวล
- ระบบประเภทที่สื่อความหมายได้ - Rust ช่วยป้องกันข้อบกพร่องทางตรรกะในการเขียนโปรแกรมด้วยการอนุญาตให้ใช้ประเภทที่สื่อความหมายได้สูง (เช่น Wrapper ของ Newtype และตัวแปรของ Enum ที่มีเนื้อหา)
- การตรวจสอบเวลาคอมไพล์ที่มีประสิทธิภาพมากขึ้น - การพบข้อบกพร่องเพิ่มเติมเมื่อคอมไพล์จะช่วยเพิ่มความมั่นใจให้กับนักพัฒนาซอฟต์แวร์ว่าเมื่อคอมไพล์โค้ดสำเร็จแล้ว โค้ดจะทํางานตามที่ตั้งใจไว้
- เฟรมเวิร์กการทดสอบในตัว - Rust มีเฟรมเวิร์กการทดสอบในตัวที่คุณสามารถวางการทดสอบหน่วยไว้ข้างๆ การใช้งานที่ทดสอบได้ ทำให้การรวมการทดสอบหน่วยทำได้ง่ายขึ้น
- การบังคับใช้การจัดการข้อผิดพลาด - ฟังก์ชันที่เกิดความล้มเหลวซึ่งแก้ไขได้จะแสดงประเภทผลลัพธ์ ซึ่งจะเป็นตัวแปรที่สำเร็จหรือตัวแปรข้อผิดพลาด คอมไพเลอร์กำหนดให้ผู้เรียกตรวจสอบและจัดการตัวแปรข้อผิดพลาดของ
Result
enum ที่แสดงผลจากการเรียกฟังก์ชัน วิธีนี้ช่วยลดโอกาสที่จะเกิดข้อบกพร่องที่เกิดจากข้อผิดพลาดที่ไม่ได้รับการจัดการ
- การเริ่มต้น - Rust กําหนดให้ต้องเริ่มต้นตัวแปรทุกรายการเป็นสมาชิกที่ถูกต้องของประเภทก่อนใช้งาน เพื่อป้องกันการเริ่มต้นเป็นค่าที่ไม่ปลอดภัยโดยไม่ตั้งใจ
- การจัดการจำนวนเต็มอย่างปลอดภัยยิ่งขึ้น - การแปลงประเภทจำนวนเต็มทั้งหมดเป็นการแคสต์ที่ชัดเจน
นักพัฒนาซอฟต์แวร์จะแคสต์โดยไม่ตั้งใจระหว่างการเรียกใช้ฟังก์ชันเมื่อกําหนดค่าให้กับตัวแปร หรือเมื่อพยายามทําการคํานวณกับประเภทอื่นๆ ไม่ได้ การตรวจสอบการเกินค่าจะเปิดอยู่โดยค่าเริ่มต้นใน Android สำหรับ Rust ซึ่งกำหนดให้การดำเนินการที่ทำให้เกิดการเกินค่าต้องระบุไว้อย่างชัดเจน
ดูข้อมูลเพิ่มเติมได้ที่ชุดบล็อกโพสต์เกี่ยวกับการรองรับ Rust ของ Android
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Android Rust introduction\n\nThe Android platform provides support for developing native OS components in Rust,\na modern systems-programming language that provides memory safety guarantees with\nperformance equivalent to C/C++. Rust uses a combination of compile-time checks\nthat enforce object lifetime and ownership, and runtime checks that ensure valid\nmemory accesses, thereby eliminating the need for a garbage collector.\n\nRust provides a range of modern language features which allow developers to be\nmore productive and confident in their code:\n\n- **Safe concurrent programming** - The ease with which this allows users to write efficient, thread-safe code has given rise to Rust's [Fearless Concurrency](https://doc.rust-lang.org/book/ch16-00-concurrency.html) slogan.\n- **Expressive type system** - Rust helps prevent logical programming bugs by allowing for highly expressive types (such as Newtype wrappers, and enum variants with contents).\n- **Stronger Compile-time Checks** - More bugs caught at compile-time increases developer confidence that when code compiles successfully, it works as intended.\n- **Built-in Testing Framework** - Rust provides a built-in testing framework where unit tests can be placed alongside the implementation they test, making unit testing easier to include.\n- **Error handling enforcement** - Functions with recoverable failures can return a [Result type](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html), which will be either a success variant or an error variant. The compiler requires callers to check for and handle the error variant of a `Result` enum returned from a function call. This reduces the potential for bugs resulting from unhandled failures.\n- **Initialization** - Rust requires every variable to be initialized to a legal member of its type before use, preventing an unintentional initialization to an unsafe value.\n- **Safer integer handling** - All integer-type conversions are explicit casts. Developers can't accidentally cast during a function call when assigning to a variable, or when attempting to do arithmetic with other types. Overflow checking is on by default in Android for Rust, which requires overflow operations to be explicit.\n\nFor more information, see the series of blog posts on Android Rust support:\n\n- [Rust in the Android Platform](https://security.googleblog.com/2021/04/rust-in-android-platform.html) \n Provides an overview on why the Android team introduced Rust as a new platform language.\n- [Integrating Rust into the Android Open Source Project](https://security.googleblog.com/2021/05/integrating-rust-into-android-open.html) \n Discusses how Rust support has been introduced to the build system, and why certain design decisions were made.\n- [Rust/C++ interop in the Android Platform](https://security.googleblog.com/2021/06/rustc-interop-in-android-platform.html) \n Discusses the approach to Rust/C++ interoperability within Android."]]