Skip to content

Commit b4a05aa

Browse files
committed
Docs(preamble): Small changes
1 parent 5960df0 commit b4a05aa

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

docs/writing-reactors/preambles.mdx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,16 @@ main reactor {
464464
}
465465
```
466466
This will print:
467+
467468
```
468469
add macro example: 2
469470
add 42 example: 52
470471
```
471472

472-
By having the `preamble` inside the reactor, you can define any functions or macros that are shared within the given reactor. This will allow you to reuse code across different reactions easily within the same reactor. This also can extend to `use` to shorten declaration of module items. For example, the follow reactor brings the constant `PI` into scope and print out the first 15 digits of pi:
473+
By having the `preamble` inside the reactor, you can define any functions or macros to be shared within the given reactor.
474+
This will allow you to reuse code across different reactions easily within the same reactor.
475+
This also can allow us to change the scope of items using the 'use' statement.
476+
For example, the follow reactor brings the constant `PI` into scope and print out the first 15 digits of pi:
473477

474478
```lf-c
475479
target Rust;
@@ -484,15 +488,19 @@ main reactor {
484488
=}
485489
}
486490
```
491+
487492
This will print:
493+
488494
```
489495
3.141592653589793
490496
```
491-
By putting `use` in the `preamble`, you can change the scope of `PI` for the for the reactor, in this case the main reactor. Its important to note that besides the crates specified in the target declarations _cargo-dependencies_ and user modules in _rust-include_, all user code is added to a module called `reactors` during compilation time. This means that crate level attributes like `#![no_std]` will not work if added to the preamble.
497+
By putting `use` in the `preamble`, the scope of `PI` is changed for the entire reactor, in this case the main reactor.
498+
This works with all items that are brought into scope using the `use` statement and not just constants and functions.
492499

493-
This would be an example of accessing a user created module named `foo` that is in the same directory as the lingua france program:
500+
This would be an example of accessing a user created module named `foo` that is included using the `rust-include` target parameter:
494501

495502
File: example.lf
503+
496504
```lf-c
497505
target Rust {
498506
rust-include: ["foo.rs"]
@@ -506,17 +514,24 @@ main reactor {
506514
}
507515
}
508516
```
517+
509518
File: foo.rs
519+
510520
```rust
511521
pub fn world() {
512522
println!("Hello, world!");
513523
}
514524
```
525+
515526
This will print:
527+
516528
```
517529
Hello, world!
518530
```
519531

532+
Its important to note that besides the crates specified in the target declarations _cargo-dependencies_ and user modules in _rust-include_, all user code is added to a module called `reactors` during compilation time.
533+
This means that crate level attributes like `#![no_std]` will not work if added to the preamble.
534+
520535
:::danger
521536
FIXME: Add `preamble` example.
522537
:::

0 commit comments

Comments
 (0)