<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e" -->

---
title: How Libraries Run Rust Inside Python (with PyO3) | daily.dev
description: Building a Rust-to-Python bridge with PyO3 by writing a Rust JSON parser and exposing it as an importable Python module. Walks through the four-step process...
canonical: https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How Libraries Run Rust Inside Python (with PyO3) | daily.dev
og:description: Building a Rust-to-Python bridge with PyO3 by writing a Rust JSON parser and exposing it as an importable Python module. Walks through the four-step process...
og:url: https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e
og:image: https://api.daily.dev/og/posts/xYFVB227E.png
og:image:alt: How Libraries Run Rust Inside Python (with PyO3)
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How Libraries Run Rust Inside Python (with PyO3)

**[Planet Python](https://daily.dev/sources/planetpython)** · 6 min read · 1 upvotes · 0 comments

## Summary

Building a Rust-to-Python bridge with PyO3 by writing a Rust JSON parser and exposing it as an importable Python module. Walks through the four-step process (write Rust module, annotate with PyO3 macros, compile with maturin, import into Python), then explains why converting the parsed Rust tree into Python dicts and lists via the IntoPyObject trait can cost more than the parsing itself. Covers error translation across the Rust/Python boundary using From<JsonError> for PyErr, and closes with advice to profile the object-materialization boundary rather than just the parsing algorithm, suggesting lazy Rust-backed views as an architectural alternative for large structures.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://belderbos.dev/blog/how-libraries-run-rust-inside-python>

## Questions this post answers

### Why is my PyO3 Rust extension slower than expected even though the Rust parsing logic itself is fast?

The bottleneck is usually the conversion step, not the parsing. When a Rust function returns a large structure like a JSON tree, PyO3's IntoPyObject trait has to walk the entire tree and rebuild it as native Python objects, creating a dict per object, a list per array, and a float or str per leaf. For a document with 100,000 values, that means roughly 100,000 Python object allocations happening after parsing is already done, which can dominate total runtime.

_Anyone optimizing a Rust extension for Python finds these boundary-cost breakdowns curated on daily.dev._

### How do you expose a Rust function to Python using PyO3?

Annotate the Rust function with the #[pyfunction] macro and register it inside a module annotated with #[pymodule]; these macros rewrite the function to add glue code handling type conversions and reference counting at the Python/Rust boundary. Maturin then compiles the crate into a shared library (.so, .dylib, or .dll) and installs it into the virtual environment so it can be imported like any normal package.

_Developers wiring Rust into Python projects track PyO3 and maturin workflow guides on daily.dev._

### How can I avoid the cost of converting a large Rust data structure into Python objects in PyO3?

Instead of materializing the entire structure into Python objects up front, hand back a lazy, Rust-backed view and build Python objects on demand only for the parts the caller actually touches. Preallocating a PyDict can help at the margins, but the larger architectural win is skipping full materialization of a tree the caller won't fully use.

_Teams weighing Rust extension design tradeoffs follow performance patterns like this on daily.dev._

## Similar posts on daily.dev

- [How to Call Rust from Python](https://daily.dev/posts/how-to-call-rust-from-python-bin3ypm03) · Towards Data Science · 5 upvotes · 0 comments
- [Getting Started with Rust as Python Devs](https://daily.dev/posts/getting-started-with-rust-as-python-devs-plu9avwmr) · Planet Python · 3 upvotes · 0 comments
- [Combining Rust and Python for High-Performance AI Systems](https://daily.dev/posts/combining-rust-and-python-for-high-performance-ai-systems-ognelzoy3) · The New Stack · 0 upvotes · 0 comments
- [Rust vs. Python: Finding the right balance between speed and simplicity](https://daily.dev/posts/rust-vs-python-finding-the-right-balance-between-speed-and-simplicity-kwyecssjv) · JetBrains · 3 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#rust](https://daily.dev/tags/rust)

[View this post on daily.dev](https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How Libraries Run Rust Inside Python (with PyO3)","url":"https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e"},"datePublished":"2026-09-13T10:51:58.912Z","dateModified":"2026-09-13T10:52:21.440Z","description":"Building a Rust-to-Python bridge with PyO3 by writing a Rust JSON parser and exposing it as an importable Python module. Walks through the four-step process...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c65d05754750cde9751e6af0cbc7f6e5?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c65d05754750cde9751e6af0cbc7f6e5?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet Python","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet Python","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/eac3cc584cb043b5a242d13752bd3d0c","url":"https://daily.dev/sources/planetpython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,rust","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet Python","item":"https://daily.dev/sources/planetpython"},{"@type":"ListItem","position":3,"name":"How Libraries Run Rust Inside Python (with PyO3)"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-libraries-run-rust-inside-python-with-pyo3--xyfvb227e#faq","mainEntity":[{"@type":"Question","name":"Why is my PyO3 Rust extension slower than expected even though the Rust parsing logic itself is fast?","acceptedAnswer":{"@type":"Answer","text":"The bottleneck is usually the conversion step, not the parsing. When a Rust function returns a large structure like a JSON tree, PyO3's IntoPyObject trait has to walk the entire tree and rebuild it as native Python objects, creating a dict per object, a list per array, and a float or str per leaf. For a document with 100,000 values, that means roughly 100,000 Python object allocations happening after parsing is already done, which can dominate total runtime. Anyone optimizing a Rust extension for Python finds these boundary-cost breakdowns curated on daily.dev."}},{"@type":"Question","name":"How do you expose a Rust function to Python using PyO3?","acceptedAnswer":{"@type":"Answer","text":"Annotate the Rust function with the #[pyfunction] macro and register it inside a module annotated with #[pymodule]; these macros rewrite the function to add glue code handling type conversions and reference counting at the Python/Rust boundary. Maturin then compiles the crate into a shared library (.so, .dylib, or .dll) and installs it into the virtual environment so it can be imported like any normal package. Developers wiring Rust into Python projects track PyO3 and maturin workflow guides on daily.dev."}},{"@type":"Question","name":"How can I avoid the cost of converting a large Rust data structure into Python objects in PyO3?","acceptedAnswer":{"@type":"Answer","text":"Instead of materializing the entire structure into Python objects up front, hand back a lazy, Rust-backed view and build Python objects on demand only for the parts the caller actually touches. Preallocating a PyDict can help at the margins, but the larger architectural win is skipping full materialization of a tree the caller won't fully use. Teams weighing Rust extension design tradeoffs follow performance patterns like this on daily.dev."}}]}
```

