Slide 1

Slide 1 text

Hello Jacek Ryś Senior Android Engineer @ MYO Solutions Branches Automotive eCommerce Finance Security IoT Logistics

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Android Rust Bridge Android Rust Bridge

Slide 4

Slide 4 text

Nice to meet YOU! Nice to meet YOU!

Slide 5

Slide 5 text

Agenda • • • • • What is Rust? Basic Concepts in Rust What about C++? Rust in Android projects Summary

Slide 6

Slide 6 text

Rust’s greatest ambition is to eliminate the trade-offs that programmers have accepted for decades by providing safety and productivity, speed and ergonomics. – Steve Klabnik and Carol Nichols, "The Rust Programming Book" “

Slide 7

Slide 7 text

What is Rust? • • • • • • • • • • • • • • Systems programming language Focus on safety, performance, and concurrency Memory safety without garbage collection Ownership and borrowing system Strong, static type system Supports many platforms x86, ARM, WebAssembly Linux, Mac, Windows Used for wide range of devices firmware and boot loaders smart displays mobile phones desktops servers

Slide 8

Slide 8 text

From personal project to systems programming revolution Conception and Development • • • Graydon Hoare starts personal project Goal: "Create a language that combines C and C++ performance with modern language features" Support from Mozilla Official Release • After several years version 1.0 is released in May 2015 Continuous Improvement • • • Improvements and updates Growth of community Focus on stability, backward compatibility and involvement of the community Growing Popularity and Adoption • • • Increased adoption in various domains Rust used in Polkadot, Mozilla, Dropbox, Discord, Claudflare, Microsoft Developers looking for security, performance and secure concurency find Rust as a solution Rust history 📝 🚀 🔄 📈

Slide 9

Slide 9 text

Developers seem to love Rust Let's check out the results from StackOverflow Surveys from last couple of years

Slide 10

Slide 10 text

2015 StackOverflow Developer Survey Most loved languages Most wanted languages % of devs who are developing with the language or tech that have expressed interest in continuing to develop with it. % of devs who are not developing with the language or tech but have expressed interest in developing with it. Rust 2015 Edition: estabished with Rust 1.0 in May 2015

Slide 11

Slide 11 text

2016 StackOverflow Developer Survey Most loved languages Most wanted languages

Slide 12

Slide 12 text

2017 StackOverflow Developer Survey Most loved languages Most wanted languages

Slide 13

Slide 13 text

2018 StackOverflow Developer Survey Most loved languages Most wanted languages

Slide 14

Slide 14 text

2019 StackOverflow Developer Survey Most loved languages Most wanted languages

Slide 15

Slide 15 text

2020 StackOverflow Developer Survey Most loved languages Most wanted languages

Slide 16

Slide 16 text

2021 StackOverflow Developer Survey Loved vs. dreaded languages Most wanted languages

Slide 17

Slide 17 text

2022 StackOverflow Developer Survey Loved vs. dreaded languages Most wanted languages

Slide 18

Slide 18 text

Basic concepts • • • • crate trait mod unsafe Keywords • • • • • • • • mut: Indicates that a binding or reference is mutable, meaning its value can be changed. dyn: Used to indicate a dynamic (trait object) type. A trait object is a runtime instance of a type implementing a specific trait. mpl: Used for implementing a trait for a specific type or defining inherent methods on a type. move: A keyword used in closures to take ownership of captured variables. pub: Specifies that an item (e.g., function, module, struct, or enum) should be publicly accessible from outside the current module. ref: Used in pattern matching to create a reference to the matched value. Self (capitalized): Refers to the type implementing a trait within the trait definition or implementation block. use: Used to import items (functions, structs, traits, etc.) from other modules or crates into the current scope.

Slide 19

Slide 19 text

Hello World

Slide 20

Slide 20 text

Main tools used for programming in Rust • • • • • • rustc - The Compiler cargo - The Package Manager - crates.io rustup - The Installer rust-analyzer - The Code Analyzer Clippy - The Linter rustfmt - The Formatter

Slide 21

Slide 21 text

Compiler Guarantees • • • • • • • No uninitialized variables No accidental memory leaks (leaks on purpose are still possible with use of Box::leak or std::mem:forget) No double-frees No use-after-free No NULL pointers 🎉 No forgotten locked mutexes No data races between threads

Slide 22

Slide 22 text

Rust is like…

Slide 23

Slide 23 text

Ownership

Slide 24

Slide 24 text

Borrowing Immutable Borrowing

Slide 25

Slide 25 text

Borrowing Mutable Borrowing

Slide 26

Slide 26 text

Rust vs C++ C++ Rust • use after free" errors are still possible despite of smart pointers introduced in C++ 11 • • C++ and C are the "unsafe" languages as they let programmers have full control over howe they manage an app's memory pointers with no restrictions or warnings on basic memory management errors C++ compilers do not provide automatic checks for data races, so bugs may go unnoticed until runtime, potentially leading to production issues or security vulnerabilities. • • • borrow checker ensures that references do not outlive data which they refer to. Such problems are detected at compile time each reference has a lifetime where you can set the scope for which that reference is valid Rust gives you a choice of writing Safe on Unsafe Rust code (the unsafe keyword) • Rust provides compile-time guarantees to prevent data races. "In C, you publish your API if it's possible to use it correctly (open world). In Rust, you publish a safe API if it's impossible to use incorrectly (closed world).” @dtolnay/cxx library is a tool and library for seamless interoperability between Rust and C++. It enables developers to write Rust code that can directly call C++ code and vice versa, without the need for manually writing complex and error-prone FFI (Foreign Function Interface) bindings

Slide 27

Slide 27 text

Why Rust in Android development? • • • • • • Improved memory safety and security Better performance Integration with Existing Java/Kotlin Codebase Interoperability with existing C/C++ code Growing community and ecosystem and more… “In Rust, the compiler plays a gatekeeper role by refusing to compile code with these elusive bugs, including concurrency bugs. By working alongside the compiler, the team can spend their time focusing on the program’s logic rather than chasing down bugs.” – Steve Klabnik and Carol Nichols, "The Rust Programming Book"

Slide 28

Slide 28 text

Example of Rust and Android projects bridged via JNI A quick demo

Slide 29

Slide 29 text

Rust - Android project 101 1. 2. 3. 4. 5. 6. 7. 8. Create Android Project​ Create Rust library Binary crate is a executable project, here we need library to put list of components used elsewhere in the project. Download Android NDK​ Define rust library type in Cargo.toml​ The cdylib crate type will produce a dynamic system library that's suitable for use via FFI from another programming language, in our case - JNI. Define linkers for all android targets You need to do it To tell Rust build system to use Android NDK linkers when building for Android platform. Add Rust code Use Rust based library in Android project Profit

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Find project on GitHub @mastahnish/rustandroidbridgeapp

Slide 32

Slide 32 text

Pros of Rust in Android development • • • • • • • • Memory safety guarantees reduce crashes and security vulnerabilities Concurrency support improves performance in multi-core systems Modern syntax and error messages enhance developer experience Growing ecosystem and community Lack of garbage collector predictable performance - no unpredictable pauses that can occur when the GC decides to reclaim memory no overhead of GC - A garbage collector continuously uses CPU cycles to track and deallocate unused memory. avoidance of common GC issues - no memory leaks. In Rust, it's prevented by design

Slide 33

Slide 33 text

Cons of Rust in Android development • • • • Learning curve for developers new to Rust It may be annoying to work with the compiler at first time Smaller ecosystem compared to C++ Knowledge of C++ is not mandadatory but would make it easier to dive into Rust

Slide 34

Slide 34 text

How to get started with Rust in Android 1. 2. 3. 4. 5. 6. Learn Rust fundamentals (resources: The Rust Programming Language book, Rust by Example, Rustlings) Set up Rust development environment (Rust toolchain, Cargo) Integrate Rust libraries with JNI/NDK for seamless interoperability Use the 'cargo-ndk' tool for building Rust libraries for Android Explore existing libraries and projects (Android-Rust-NDK, Rust-Android-Gradle) https://google.github.io/comprehensive-rust/

Slide 35

Slide 35 text

Conclusion Change perspective • • • • • • broadening skillset performance improvement memory safety interoperability professional growth community and support

Slide 36

Slide 36 text

Links https://doc.rust-lang.org/book https://doc.rust-lang.org/reference https://www.rust-lang.org/community https://github.com/rust-mobile/ndk https://cheats.rs/ https://google.github.io/comprehensive-rust/ https://codilime.com/blog/

Slide 37

Slide 37 text

Check out Android portfolio www.myosolutions.pl Portfolio

Slide 38

Slide 38 text

Thank You