summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/files/maturin/guessing-game/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oeqa/files/maturin/guessing-game/src/lib.rs')
-rw-r--r--meta/lib/oeqa/files/maturin/guessing-game/src/lib.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/meta/lib/oeqa/files/maturin/guessing-game/src/lib.rs b/meta/lib/oeqa/files/maturin/guessing-game/src/lib.rs
new file mode 100644
index 0000000000..6828466ed1
--- /dev/null
+++ b/meta/lib/oeqa/files/maturin/guessing-game/src/lib.rs
@@ -0,0 +1,48 @@
1use pyo3::prelude::*;
2use rand::Rng;
3use std::cmp::Ordering;
4use std::io;
5
6#[pyfunction]
7fn guess_the_number() {
8 println!("Guess the number!");
9
10 let secret_number = rand::thread_rng().gen_range(1..101);
11
12 loop {
13 println!("Please input your guess.");
14
15 let mut guess = String::new();
16
17 io::stdin()
18 .read_line(&mut guess)
19 .expect("Failed to read line");
20
21 let guess: u32 = match guess.trim().parse() {
22 Ok(num) => num,
23 Err(_) => continue,
24 };
25
26 println!("You guessed: {}", guess);
27
28 match guess.cmp(&secret_number) {
29 Ordering::Less => println!("Too small!"),
30 Ordering::Greater => println!("Too big!"),
31 Ordering::Equal => {
32 println!("You win!");
33 break;
34 }
35 }
36 }
37}
38
39/// A Python module implemented in Rust. The name of this function must match
40/// the `lib.name` setting in the `Cargo.toml`, else Python will not be able to
41/// import the module.
42#[pymodule]
43fn guessing_game(_py: Python, m: &PyModule) -> PyResult<()> {
44 m.add_function(wrap_pyfunction!(guess_the_number, m)?)?;
45
46 Ok(())
47}
48