-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdoublecount.rs
41 lines (34 loc) · 862 Bytes
/
doublecount.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// rustimport
//: [package]
//: name = "doublecount"
//: version = "0.1.0"
//: authors = ["Bruno Rocha <[email protected]>"]
//:
//: [lib]
//: name = "myrustlib"
//: crate-type = ["cdylib"]
//:
//: [dependencies.cpython]
//: version = "0.7"
//: features = ["extension-module"]
#[macro_use]
extern crate cpython;
use cpython::{Python, PyResult};
fn count_doubles(_py: Python, val: &str) -> PyResult<u64> {
let mut total = 0u64;
let mut chars = val.chars();
if let Some(mut c1) = chars.next() {
for c2 in chars {
if c1 == c2 {
total += 1;
}
c1 = c2;
}
}
Ok(total)
}
py_module_initializer!(doublecount, |py, m | {
m.add(py, "__doc__", "This module is implemented in Rust.")?;
m.add(py, "count_doubles", py_fn!(py, count_doubles(val: &str)))?;
Ok(())
});