Quickstart
If you like to setup in one command:
cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0' && flutter_rust_bridge_codegen create my_app && cd my_app && flutter run
1. Install
After Flutter and Rust are installed,
install flutter_rust_bridge
using any method:
- Default
- Cargo-Binstall
- Scoop
- Homebrew
cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0'
cargo binstall 'flutter_rust_bridge_codegen@^2.0.0-dev.0'
It installs v1 instead of v2.0.0-dev.x currently, so please wait a bit until the v2.0.0 stable is released :)
scoop bucket add frb https://github.com/Desdaemon/scoop-repo
scoop install flutter_rust_bridge_codegen
It installs v1 instead of v2.0.0-dev.x currently, so please wait a bit until the v2.0.0 stable is released :)
brew install desdaemon/repo/flutter_rust_bridge_codegen
2. Create new projects / Add to existing projects
- Create new
- Add to existing
- Pure dart
- More approaches
Suppose your app is to be named my_app
, then execute this. (Use --help
to see more options, e.g. package name)
flutter_rust_bridge_codegen create my_app
Execute the following in the root folder of your Flutter project:
flutter_rust_bridge_codegen integrate
Please refer to this page.
Please visit this page.
Remark: Directory structure
Though seemingly many files, it is pretty simple:
"a standard Flutter app + a standard Rust crate + some glues to ignore".
In short, write Flutter near lib/main.dart
and Rust near rust/src/api/simple.rs
.
For more details, please see this page.
3. Run it
- Default
- Web
Use your favorite method to run the app (Flutter official doc), as if it is just a normal Flutter project. For example:
flutter run
- The
build-web
command: Because Flutter Web does not have a build hook yet (vote the corresponding issue to prioritize it if you want). - Before Flutter >=3.17 release, a hack is needed.
flutter_rust_bridge_codegen build-web
flutter run # or any other standard Flutter ways
Then, you will see a greeting from Rust, displayed in Flutter (Dart).
4. Modify it
Suppose we add a super-simple Rust function in rust/src/api/simple.rs
(see next chapter for all features):
pub fn hello(a: String) -> String { a.repeat(2) }
With all glue code automatically generated, we can call it in Dart (lib/main.dart
):
var result = await hello(a: "Hi");
Explain the Dart code
- The
await
is for asynchronous code, a very frequently used feature in Dart. - To display the result on the screen, a bit of standard Flutter knowledge may be needed. See the existing code for an example how a String can be shown.
We need to execute the code generator whenever the Rust code is changed,
or use --watch
to automatically re-generate when code changes:
flutter_rust_bridge_codegen generate --watch
What's next
On one hand, if you like to see a live demo, please visit the next page.
On the other hand, the guides chapter introduces all features, customizations, common scenario how-tos, etc. There are a lot of documentations, but there is no need to learn all in details. Instead:
Use intuition for flutter_rust_bridge - the ideal bridge between Rust and Dart should be seamless, just like using one single language.
Refer to documentation when curious to know more details, or when some syntax boilerplates are needed.