This project demonstrates the integration between Rust and Python(w Fastapi) using PyO3 for Foreign Function Interface (FFI). It compares the performance of a Fibonacci function implemented in both Rust and Python, where a FastAPI endpoint calls the Rust function for computation.
Implementation | Result | Time (seconds) |
---|---|---|
Rust | 102334155 | 0.353371 |
Python | 102334155 | 29.310354 |
- Description: Calculates the Fibonacci sequence for a given number
n
using the Rust implementation. - Method:
GET
- Path Parameters:
n
(integer): The number for which to calculate the Fibonacci sequence.
- Response:
{ "result": 102334155, "time": 0.35337138175964355 }
- Description: Calculates the Fibonacci sequence for a given number
n
using the Python implementation. - Method:
GET
- Path Parameters:
n
(integer): The number for which to calculate the Fibonacci sequence.
- Response:
{ "result": 102334155, "time": 29.310354709625244 }
Siga os passos abaixo para configurar e executar o projeto:
git clone https://github.com/seu-usuario/rust-py-ffi.git
cd rust-py-ffi
Certifique-se de ter o uv
instalado. Se não tiver, pode instalá-lo com pip install uv
.
Em seguida, sincronize as dependências e o ambiente virtual:
uv sync
O maturin
será usado para compilar o código Rust e torná-lo disponível para o Python. Execute o comando develop
para um ambiente de desenvolvimento:
maturin develop
Com a extensão Rust compilada e o ambiente Python configurado, você pode iniciar a aplicação FastAPI:
uv run fastapi dev src/main.py --port 8000 --reload
A aplicação estará acessível em http://localhost:8000
. Você pode ir para http://localhost:8000/docs
para ver a documentação interativa da API.