DEV Community

Cover image for Rust once and share it with Android, iOS and Flutter

Rust once and share it with Android, iOS and Flutter

Roberto Huertas on October 26, 2019

What if I told you that you could use the same very performant code in Android, iOS or even in Flutter. In this article, we’ll see how to achieve t...
Collapse
 
zoechi profile image
Günter Zöchbauer

Great article!
Flutter (Dart) has experimental FFI support that should allow to integrate Rust without the intermediate JNI step

Collapse
 
robertohuertasm profile image
Roberto Huertas

You're right! Probably I'll dedicate a specific post for that. As it is experimental and the API can still change I thought it would be better to focus on a more stable way to integrate Rust with Flutter. Thanks for your insight! 😊

Collapse
 
iapicca profile image
Francecsco Iapicca

I was thinking the same thing, it's a shame that most of portability to flutter are just wraps around Android code,
While it's possible achieve the same in a cleaner and more efficient way

Collapse
 
piannaf profile image
Justin Mancinelli

I love what Rust is doing in the multiplatform space (especially with Wasm). Thank you for highlighting more of what's possible.

Have you also looked into Kotlin Multiplatform? It can also output an iOS framework, and C library. On Android, it's nice because you can benchmark whether your code would work just fine in JVM-land (ART) or if you'd be better off compiling to native code and dealing with JNI overhead.

Jake Wharton gave a talk that broached this topic at Droidcon NYC 2019 droidcon.com/media-detail?video=36...

Rust may outperform compiled Kotlin code, but it's something to keep in mind.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
doddzilla profile image
Anthony Dodd

Well done! I love seeing stuff like this. Rust is the perfect language for bridging all of these gaps, IMHO.

Collapse
 
jvr0x profile image
Javier ⚛️

Very interesting indeed, well done! I wonder what are the setbacks you see on going multiplatform with rust? I have seen using non-conventional languages for writing once - running everywhere before and even though it looks cool, how maintainable is this in terms of finding developers willing to work with such project, third-party libraries, community... I see it as a cool experiment but not something you could do in a long run.

Collapse
 
robertohuertasm profile image
Roberto Huertas

Hi Javier, I wouldn't certainly write a whole app using this approach, but for a very specific need in which you want to share code in the form of a native library I would. This, of course, would ultimately depend on the specifics of your team and your requirements. I don't think it would be a difficult task to find some devs interested in maintaining this part of the app. There's always someone willing to tackle this kind of complexities. The alternative in these kind of cases is having a dedicated team per platform, or do it in C++. Code duplication is costly, also in terms of possible bugs, and if you're willing to jump into C++... then why not use a safer programming language if possible.

As usual, there's not really a correct answer and it's ultimately you who have to decide, considering lots of factors, if it would be suitable for you or not.

Collapse
 
silmanduin66 profile image
silmanduin66

Thank you for this interesting post. I'm currently developing apps with flutter and dart. Would i see a noticeable improvement on mobile in writing some code with rust instead of dart ? What are the real benefits here ?

Collapse
 
robertohuertasm profile image
Roberto Huertas

The main objective of the article was to show that you could share the same code between different platforms, including Flutter. That being said, I guess it would only make sense to use Rust for a very intense process or calculation so you could leverage Rust's memory management and the ability to work in a parallel way without having to use isolates.

Collapse
 
pablonax profile image
Pablo Discobar

Helpful article! Thanks! If you are interested in this, you can also look at my article about Flutter templates. I made it easier for you and compared the free and paid Flutter templates. I'm sure you'll find something useful there, too. - dev.to/pablonax/free-vs-paid-flutt...

Collapse
 
vibhoothi profile image
Vibhoothi

Great Read :)

Collapse
 
djmaze profile image
Martin Honermeyer

I wonder what's really the benefit of using Rust compared to C(++) when you need to write unsafe code either way.

Collapse
 
mergerg profile image
Raphael

I was concerned about the same thing... Like, one of the biggest pros of Rust is the safety, so I don't particularly want to be writing apps with the unsafe keyword unless I really know what I'm doing (and most following along with this probably aren't expert Rust programmers who know how to write with the "unsafe" keyword safely)

Collapse
 
novski profile image
Novski

Did i overread it or did you forget to mention that xcode is only available on a apple computer?

Collapse
 
robertohuertasm profile image
Roberto Huertas

I didn't mention it as I thought it was obvious. But maybe I should in case some people don't know this. Thanks for your comment.

Collapse
 
iampaoloxd profile image
Paolo

And for the web is wasm? And for the desktop can be native? Just starting learning rust and this is awesome i am so hype right now , thanks for sharing

Collapse
 
osein profile image
Hasan Gücüyener

Excellent article. I wonder if we can share a network layer this way between domains?

Collapse
 
andreslopezrm profile image
Andres Lopez

Fanastic article, rust is powerfull!!!

Collapse
 
erlangparasu profile image
Erlang Parasu

alternative: rust in flutter: rinf: github.com/cunarist/rinf

Collapse
 
hmvmpire profile image
Mian Hassan

Great Article !
after read this , i think there is possibility to build an app for minning(crypto) on IOS and Android via Flutter .

Collapse
 
drsensor profile image
૮༼⚆︿⚆༽つ

Noice! How about adding Nativescript plugin too 🤓