החל מ-27 במרץ 2025, מומלץ להשתמש ב-android-latest-release
במקום ב-aosp-main
כדי ליצור תרומות ל-AOSP. מידע נוסף זמין במאמר שינויים ב-AOSP.
מבוא ל-Rust ב-Android
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
פלטפורמת Android מספקת תמיכה בפיתוח רכיבים מקומיים של מערכת ההפעלה ב-Rust, שפה מודרנית לתכנות מערכות שמספקת ערבויות לאבטחת זיכרון עם ביצועים שדומים ל-C/C++. ב-Rust נעשה שימוש בשילוב של בדיקות בזמן הידור שמאכסחות את משך החיים והבעלות על אובייקטים, ובדיקות בסביבת זמן הריצה שמבטיחות גישה תקינה לזיכרון, וכך מבטלות את הצורך באיסוף אשפה.
ב-Rust יש מגוון תכונות מודרניות של שפה שמאפשרות למפתחים להיות פרודוקטיביים יותר ובטוחים יותר בקוד שלהם:
- תכנות בטוחה במקביל – הקלילות שבה אפשר לכתוב קוד יעיל ומאובטח לשרשור (thread-safe) הובילה ליצירת הסלוגן של Rust, Fearless Concurrency.
- מערכת סוגים יעילה – Rust עוזרת למנוע באגים לוגיים בתוכנה על ידי מתן אפשרות לשימוש בסוגי נתונים יעילים מאוד (כמו חבילות של Newtype ווריאנטים של enum עם תוכן).
- בדיקות חזקות יותר בזמן הידור – ככל שיותרים באגים בזמן הידור, כך המפתחים יכולים להיות בטוחים יותר שהקוד יפעל כמצופה אחרי הידור.
- מסגרת בדיקה מובנית – ב-Rust יש מסגרת בדיקה מובנית שבה אפשר למקם בדיקות יחידה לצד ההטמעה שהן בודקות, כך שקל יותר לכלול בדיקות יחידה.
- אכיפה של טיפול בשגיאות – פונקציות עם כשלים שניתן לשחזר יכולות להחזיר סוג תוצאה, שיהיה וריאנט של הצלחה או וריאנט של שגיאה. המהדרר מחייב את מבצעי הקריאה לבדוק את וריאנט השגיאה של
Result
enum שמוחזר מקריאה לפונקציה ולטפל בו. כך מצטמצם הסיכון לבאגים שנובעים מכשלים שלא טופלו.
- הפעלה – ב-Rust כל משתנה צריך להיות מופעל לערך חוקי מהסוג שלו לפני השימוש, כדי למנוע הפעלה לא מכוונת לערך לא בטוח.
- טיפול בטוח יותר במספרים שלמים – כל ההמרות מסוג מספר שלם הן המרות מפורשות.
המפתחים לא יכולים לבצע הטמעה בטעות במהלך קריאה לפונקציה, כשמקצים למשתנה או כשמנסים לבצע פעולות חשבון עם סוגים אחרים. בדיקת Overflow מופעלת כברירת מחדל ב-Android עבור Rust, וכתוצאה מכך פעולות Overflow צריכות להיות מפורשות.
למידע נוסף, אפשר לעיין בסדרת הפוסטים בבלוג על תמיכה ב-Rust ל-Android:
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון UTC).
[null,null,["עדכון אחרון: 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."]]