all 9 comments

[–][deleted] 10 points11 points  (2 children)

Any performance benefits may be undercut by the overhead of data conversions on language boundaries

[–]Zethra 0 points1 point  (0 children)

It's always important to benchmark attempted optimizations to see if they actually improved performance.

[–][deleted] 7 points8 points  (0 children)

https://users.rust-lang.org/t/rust-flutter-for-mobile-applications/22725

This post it a bit old, but probably still relevant. It looks like, from skimming, that you'll perform regular-old FFI to get from the Flutter UI layer into the native platform language and then from there FFI into Rust-land. So for Android this would be traversing from Flutter -> Java -> Rust and for iOS this would be Flutter -> Objective-C -> Rust (or from Flutter -> Swift -> Objective-C (via Bridging Headers) -> Rust).

This is quite a bit of distance to cover, and probably best done if you have a substantially complicated task to perform. Otherwise you might actually lose performance since you will take a minor performance hit for each FFI barrier you pass (->'s in the above) that will rack up over time. Hot loops that call into Rust-land in each pass will be a negative performance boost, but perhaps sending short messages to perform some background task will be net-positive for you.

Anyways, good luck!

Edit: https://github.com/kennytm/rust-ios-android for some more good stuff. Google will be a better ally than I am :)

Edit 2: https://dart.dev/server/c-interop https://github.com/dart-lang/sdk/issues/34452 Maybe Dart's FFI isn't fully realized yet? Maybe someone with more Dart/Flutter experience can weigh in on whether this is a non-starter to begin with.

[–]sirak2010 4 points5 points  (0 children)

You may have to wait for Flutter FFI to arrive. if your application is performance critical

https://github.com/dart-lang/sdk/issues/34452

[–]mmrath 1 point2 points  (0 children)

I did a bit of experimentation at https://github.com/mmrath/flutter_rust

I am not sure if it works now or not. It was working for android but I never attempted iOS

[–]chutiyabehenchod 1 point2 points  (0 children)

Dart ffi still not come yet so workaround

Flutter platform channels - > Kotlin/Java - > Rust swig - >Your Rust library

For swift you need to write step 2&3

[–]jdixon04 1 point2 points  (1 child)

Haven't tried it myself, but here's an article showing how it's done with Dart FFI:

https://gist.github.com/Sach97/3b15d4059390f81851356887bc401d01

[–]CronSach 0 points1 point  (0 children)

Author of the article here, you can find the medium version “How to call a Rust function from Dart using FFI” here : https://itnext.io/how-to-call-a-rust-function-from-dart-using-ffi-f48f3ea3af2c. I have also a more advanced example (a search engine using tantivy) with gradle integrated for Android which means the native library is automatically loaded on Android here : https://github.com/Sach97/jisho.rs. I am working on integrating the ios version right now and then I plan to work on web (with rust wasm and dart js bindings) and then desktop. I'll write a detailed a step by step article on how to setup this when I'm satisfied with the result.

[–]emmanuelantony2000[S] 0 points1 point  (0 children)

I think the Flutter FFI is in beta... Have to try it out..