Update to include changes in upstream Maturin Tutorial [1]
and for newer Rust and Python.
Cargo.toml:
* version: 0.1.0 -> 0.3.0 (to align with [2])
* edition: 2021 -> 2024
* Dependencies:
- rand: 0.8.5 -> 0.9.0
- pyo3: 0.21.2 -> 0.27.2
- abi3-py38 -> abi3-py39 (Python 3.8 reached EOL in October 2024)
pyproject.toml:
* restrict maturin to >=1.7 to ensure PyO3 27.0+ and python 3.13+ support
src/lib.rs:
* rand 0.9 API change: rand::thread_rng() was removed. Use rand::rng() or
the convenience function rand::random_range().
* PyO3 0.27 has breaking API changes from 0.21. The #[pymodule] function
signature changed from fn module_name(py: Python, m: &PyModule) to
fn module_name(m: &Bound<'_, PyModule>).
With help from Claude.ai
[1] https://www.maturin.rs/tutorial.html
[2] https://github.com/moto-timo/guessing-game/releases/tag/v0.3.0
Signed-off-by: Tim Orling <tim.orling@konsulko.com>
Signed-off-by: Antonin Godard <antonin.godard@bootlin.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
[package]
name = "guessing-game"
-version = "0.1.0"
-edition = "2021"
+version = "0.3.0"
+edition = "2024"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
crate-type = ["cdylib"]
[dependencies]
-rand = "0.8.4"
+rand = "0.9.0"
[dependencies.pyo3]
-version = "0.24.1"
-# "abi3-py38" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.8
-features = ["abi3-py38"]
+version = "0.27.2"
+# "abi3-py39" tells pyo3 (and maturin) to build using the stable ABI with minimum Python version 3.9
+features = ["abi3-py39"]
[build-system]
-requires = ["maturin>=1.0,<2.0"]
+requires = ["maturin>=1.7,<2.0"]
build-backend = "maturin"
[tool.maturin]
fn guess_the_number() {
println!("Guess the number!");
- let secret_number = rand::thread_rng().gen_range(1..101);
+ let secret_number = rand::rng().random_range(1..101);
loop {
println!("Please input your guess.");
/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
/// import the module.
#[pymodule]
-fn guessing_game(_py: Python, m: &PyModule) -> PyResult<()> {
+fn guessing_game(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(guess_the_number, m)?)?;
Ok(())