Skip to content

tony/python-rust-mvp

Repository files navigation

Python-Rust-MVP

Minimal Rust-Python example using PyO3.

Setup

make dev
make test
make build

Usage

from mvp import add, Calculator

# Add two numbers
add(5, 10)

# Use Calculator
calc = Calculator(5)
calc.get_value()
calc.add(10)

Development Demo

Here's a quick demonstration of the development workflow with Rust and Python integration:

  1. Add a debug print statement to the Rust code:

    diff --git a/src/lib.rs b/src/lib.rs
    --- a/src/lib.rs
    +++ b/src/lib.rs
    @@ -20,6 +20,7 @@ impl Calculator {
         }
    
         fn add(&self, other: i64) -> PyResult<i64> {
    +        println!("🦀 FROM RUST: Calculator adding {} to {}", other, self.value);
             Ok(self.value + other)
         }
  2. Rebuild and run tests with stdout capturing disabled:

    make rebuild; uv run py.test -x -s
  3. See the Rust println! output in your Python tests:

    ======= test session starts =======
    collected 4 items
    
    tests/test_mvp.py .🦀 FROM RUST: Calculator adding 10 to 10
    ..🦀 FROM RUST: Calculator adding 5 to 10
    🦀 FROM RUST: Calculator adding 20 to 10
    .
    
    ======= 4 passed in 0.01s =======
    

This demonstrates how you can debug Rust code using println! statements that will appear in your Python test output when using pytest's -s flag.

See Also

  • nodejs-rust-mvp - A companion project showing Rust integration with Node.js using WebAssembly

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Minimal Rust-Python example using PyO3.

Topics

Resources

License

Stars

Watchers

Forks