feature image

2025年3月2日 | ブログ記事

kepeken Rust tan Python (Calling Rust from Python)

This article is the 8th day of NoJaRelay.

toki!

mi pona la, mi toki e kepeken ilo Rust tan toki Python lon sinpin sina. kepeken Rust kepeken monsi e toki Python li ike tawa lukin pona. taso, ni li pona tawa pana en pali.

sitelen e toki Rust

mi kepeken Pyo3 tawa ni. mi wile sitelen nasin kama(proc-macro).

// nasin ante(function) tawa seme tenpo
#[pyfunction]
#[pyo3(name = "seme_tenpo")] // nimi lon toki Python li "seme_tenpo"
fn current_time() -> String {
    chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
}

mi ken kepeken seme_tenpo tan toki Python.

seme_tenpo()
# ni nasin ante toki...
# `2025-03-02 12:00:00`

mi ken pali e nasin ijo(class/struct) en kulupu nimi(enum).

#[pyclass]
#[pyo3(name = "jan")]
struct Person {
    name: String,
    id: Uuid,
}

#[pymethods]
impl Person {
    #[new] // nasin ante "open" lon toki Python
    fn new(name: String) -> Self {
        Self {
            name,
            id: Uuid::new_v4(),
        }
    }
    
    // ni toki e nimi
    #[pyo3(name = "nimi")]
    fn get_name(&self) -> String {
        name.clone()
    }
}

mi tawa!

tenpo ni la, mi wile toki kepeken toki Inli, tan ni: ona li suli.

I would like to express my gratitude to participants of NoJa Relay, this blog series. I also appreciate blog-relay admins of traP who maintain reminders for blog events and wiki page templates. And, of course, I thank all of you who read blog posts published in this blog relay!

comavius icon
この記事を書いた人
comavius

この記事をシェア

このエントリーをはてなブックマークに追加
共有

関連する記事

2023年7月15日
2023 春ハッカソン 06班 stamProlog
H1rono_K icon H1rono_K
2022年9月26日
競プロしかシラン人間が web アプリ QK Judge を作った話
tqk icon tqk
2024年8月29日
クロスコンパイルRust
H1rono_K icon H1rono_K
2023年12月25日
オレオレRustプロジェクトテンプレート
H1rono_K icon H1rono_K
2023年3月30日
みやぎハッカソン2023に参加しました(ずんだ食べ食べ委員会)
mehm8128 icon mehm8128
2022年10月11日
アルゴリズム班にKaggle部を設立し、初心者向けデータ分析体験会を開催しました!
abap34 icon abap34
記事一覧 タグ一覧 Google アナリティクスについて 特定商取引法に基づく表記