-
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update Add a way to create new rust files docs: add rust docs add rust creation file update
- Loading branch information
Showing
4 changed files
with
152 additions
and
30 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
docs_src/src/pages/documentation/api_reference/using_rust_directly.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
|
||
## Using Rust to extend Robyn | ||
|
||
|
||
There may be occassions where Batman may be working with a high computation task, or a task that requires a lot of memory. In such cases, he may want to use Rust to implement that task. Robyn introduces a special way to do this. Not only you can use Rust to extend Python code, you can do it while maintaining the hot reloading nature of your codebase. Making it *feel* like an interpreted version in many situations. | ||
|
||
|
||
|
||
<Row> | ||
<Col> | ||
The first thing you need to is to create a Rust file. Let's call it `hello_world.rs`. You can do it using the cli: | ||
</Col> | ||
<Col sticky> | ||
|
||
<CodeGroup title="Request" tag="GET" label="/hello_world"> | ||
|
||
```python {{ title: 'untyped' }} | ||
python -m robyn --create-rust-file hello_world | ||
``` | ||
|
||
```python {{title: 'typed'}} | ||
python -m robyn --create-rust-file hello_world | ||
``` | ||
|
||
</CodeGroup> | ||
</Col> | ||
</Row> | ||
<Row> | ||
|
||
<Col> | ||
Then you can open the file and write your Rust code. For example, let's write a function that returns a string. | ||
|
||
|
||
</Col> | ||
<Col sticky> | ||
<CodeGroup title="Request" tag="GET" label="/hello_world"> | ||
|
||
```rust | ||
// hello_world.rs | ||
|
||
// rustimport:pyo3 | ||
|
||
use pyo3::prelude::*; | ||
|
||
#[pyfunction] | ||
fn square(n: i32) -> i32 { | ||
n * n * n | ||
// this is another comment | ||
} | ||
|
||
``` | ||
|
||
</CodeGroup> | ||
</Col> | ||
|
||
Then you can import the function in your Python code and use it. | ||
|
||
<Col sticky> | ||
<CodeGroup title="Request" tag="GET" label="/hello_world"> | ||
|
||
```python {{ title: 'untyped' }} | ||
from hello_world import square | ||
|
||
print(square(5)) | ||
``` | ||
|
||
```python {{title: 'typed'}} | ||
from hello_world import square | ||
|
||
print(square(5)) | ||
``` | ||
</CodeGroup> | ||
</Col> | ||
|
||
To run the code, you need to use the `--compile-rust-path` flag. This will compile the Rust code and run it. You can also use the `--dev` flag to watch for changes in the Rust code and recompile it on the fly. | ||
|
||
<Col sticky> | ||
<CodeGroup title="Request" tag="GET" label="/hello_world"> | ||
|
||
```python {{ title: 'untyped' }} | ||
python -m robyn --compile-rust-path "." --dev | ||
``` | ||
|
||
```python {{title: 'typed'}} | ||
python -m robyn --compile-rust-path "." --dev | ||
``` | ||
</CodeGroup> | ||
|
||
|
||
</Row> | ||
|
||
--- | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters