Building a Basic Finance App with Rust: A Simple Guide

Rahul BoneyRahul Boney
3 min read

In this article, we'll explore how to build a basic finance app using Rust. Our app will help you calculate various financial metrics based on your income. We'll use a combination of Rust for the backend logic and Slint for the user interface. Let’s dive into the code and understand how it all comes together!

What You’ll Learn

  • Setting up a Rust project with Slint

  • Building a user interface with Slint

  • Writing Rust code to perform financial calculations

  • Connecting the UI with the backend

Project Overview

Our finance app will take an income amount as input and calculate:

  • Taxes

  • Owner's share

  • Profit

  • Operating expenses

We’ll use the following constants for our calculations:

  • TAXPER: 30% tax

  • OWNERPER: 55% owner's share

  • PROFITPER: 5% profit

  • OPEXPER: 10% operating expenses

Setting Up the Project

Start by creating a new Rust project:

cargo new financeapp
cd financeapp

Add Slint to your Cargo.toml:

[dependencies]
slint = "0.4"  # Check for the latest version

Building the User Interface

We’ll define the UI in a Slint file. Create a file named ui.slint:

import { Button, VerticalBox, LineEdit } from "std-widgets.slint";

export component AppWindow inherits Window {
    in property <int> name;
    in property <string> results:"";
    in-out property <bool> opencurtain;
    callback divide-income(string);
    background: #354356;
    GridLayout {
        padding: 50px;
        spacing: 25px;
        Row {
            Text {
                color: white;
                text: "Enter the amount you got";
                horizontal-alignment: center;
                font-size: 24px;
                font-weight: 900;
            }
        }
        Row {
            income := LineEdit {
                horizontal-alignment: center;
                font-size: 16px;
                placeholder-text: "Enter your income";
                height: 35px;
            }
        }
        Row {
            Button {
                text: "Calculate";
                primary: true;
                height: 35px;
                clicked => {
                    root.opencurtain = !root.opencurtain;
                    divide-income(income.text);
                }
            }
        }
        Row {
            VerticalBox {
                Rectangle {
                    height: 100px;
                    background: white;
                    Text {
                        color: black;
                        font-size: 16px;
                        font-weight: 500;
                        text: root.results;
                    }
                    Rectangle {
                        background: black;
                        x: 0;
                        width: opencurtain ? 0px : parent.width / 2;
                        height: parent.height;
                        animate width {
                            duration: 250ms;
                            easing: ease-in;
                        }
                    }
                    Rectangle {
                        background: black;
                        x: opencurtain ? parent.width : parent.width / 2;
                        width: opencurtain ? 0px : parent.width / 2;
                        height: parent.height;
                        animate width {
                            duration: 250ms;
                            easing: ease-in;
                        }
                        animate x {
                            duration: 250ms;
                            easing: ease-in;
                        }
                    }
                }
            }
        }
    }
}

Writing the Backend Logic

Now, let’s write the Rust code to handle the financial calculations. Create or modify the main.rs file:

use slint::{include_modules, PlatformError, Weak};

include_modules!();

const TAXPER: f64 = 0.30;
const OWNERPER: f64 = 0.55;
const PROFITPER: f64 = 0.05;
const OPEXPER: f64 = 0.10;

fn main() -> Result<(), PlatformError> {
    let ui = AppWindow::new()?;
    let ui_handle: Weak<AppWindow> = ui.as_weak();

    ui.on_divide_income(move |string| {
        if let Some(ui) = ui_handle.upgrade() {
            let num: f64 = string.trim().parse().unwrap();
            let tax: f64 = num * TAXPER;
            let owner: f64 = num * OWNERPER;
            let profit: f64 = num * PROFITPER;
            let opex: f64 = num * OPEXPER;

            let result: String = format!(
                "Taxes: {:.2}\nOwner: {:.2}\nProfit: {:.2}\nOperating Expenses: {:.2}\n",
                tax, owner, profit, opex
            );
            ui.set_results(result.into());
        }
    });

    ui.run()
}

Running the App

To run your app, use:

cargo run

This command will compile and start your application. You should see a window where you can enter your income and calculate the financial metrics.

Conclusion

We’ve built a simple finance app using Rust and Slint. The Rust code handles the calculations, while the Slint UI provides a user-friendly interface. This basic setup can be expanded with more features and refinements as needed.

You can find the full code for this project in the GitHub repository: financeappusingrust.

Feel free to explore and modify the code to fit your needs!


0
Subscribe to my newsletter

Read articles from Rahul Boney directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Boney
Rahul Boney

Hey, I'm Rahul Boney, really into Computer Science and Engineering. I love working on backend development, exploring machine learning, and diving into AI. I am always excited about learning and building new things.