Back to Flutter Rust Bridge

Methods

website/docs/guides/functions/methods.md

2.12.01.2 KB
Original Source

Methods

In addition to standard functions, there is support for structs with methods. Both static methods, and non-static methods are supported. No special syntax is needed, and just write normal impl YourStruct { pub fn your_method() {} }.

For methods in other crates, please refer to this page and the more general feature.

Example

Example 1: Methods in same crate

rust
pub struct SumWith { pub x: u32 }

impl SumWith {
    pub fn sum(&self, y: u32) -> u32 { self.x + y }
    pub fn sum_static(x: u32, y: u32) -> u32 { x + y }
}

Becomes:

dart
class SumWith {
  final int x;

  const SumWith({
    required this.x,
  });

  Future<int> sum({required int y, dynamic hint}) { ... }

  static Future<int> sumStatic({required int x, required int y, dynamic hint}) { ... }
}

Remark: If you are curious about Future, have a look at this.

Example 2: Methods in external crates

The documentation was moved to this page. The new feature - automatically scanning a whole third party package - may also be helpful and is discussed here.