Skip to content

Commit 7e3f99e

Browse files
authored
Merge pull request #136 from brauliobz/grammar_expr_block
Blocks grammar
2 parents 7bfc70e + ac00207 commit 7e3f99e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/expressions/block-expr.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Block expressions
22

3+
> **<sup>Syntax</sup>**
4+
> _BlockExpression_ :
5+
> &nbsp;&nbsp; `{`
6+
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>
7+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Statement_]<sup>\*</sup>
8+
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_]<sup>?</sup>
9+
> &nbsp;&nbsp; `}`
10+
311
A _block expression_ is similar to a module in terms of the declarations that
412
are possible, but can also contain [statements](statements.html) and end with
513
an expression. Each block conceptually introduces a new namespace scope. Use
@@ -28,7 +36,27 @@ if really needed.
2836

2937
## `unsafe` blocks
3038

39+
> **<sup>Syntax</sup>**
40+
> _UnsafeBlockExpression_ :
41+
> &nbsp;&nbsp; `unsafe` _BlockExpression_
42+
3143
_See [`unsafe` block](unsafe-blocks.html) for more information on when to use `unsafe`_
3244

3345
A block of code can be prefixed with the `unsafe` keyword, to permit calling
34-
`unsafe` functions or dereferencing raw pointers within a safe function.
46+
`unsafe` functions or dereferencing raw pointers within a safe function. Examples:
47+
48+
```rust
49+
unsafe {
50+
let b = [13u8, 17u8];
51+
let a = &b[0] as *const u8;
52+
assert_eq!(*a, 13);
53+
assert_eq!(*a.offset(1), 17);
54+
}
55+
56+
# unsafe fn f() -> i32 { 10 }
57+
let a = unsafe { f() };
58+
```
59+
60+
[_InnerAttribute_]: attributes.html
61+
[_Statement_]: statements.html
62+
[_Expression_]: expressions.html

0 commit comments

Comments
 (0)