File tree 1 file changed +29
-1
lines changed
1 file changed +29
-1
lines changed Original file line number Diff line number Diff line change 1
1
# Block expressions
2
2
3
+ > ** <sup >Syntax</sup >**
4
+ > _ BlockExpression_ :
5
+ >   ;  ; ` { `
6
+ >   ;  ;   ;  ; [ _ InnerAttribute_ ] <sup >\* </sup >
7
+ >   ;  ;   ;  ; [ _ Statement_ ] <sup >\* </sup >
8
+ >   ;  ;   ;  ; [ _ Expression_ ] <sup >?</sup >
9
+ >   ;  ; ` } `
10
+
3
11
A _ block expression_ is similar to a module in terms of the declarations that
4
12
are possible, but can also contain [ statements] ( statements.html ) and end with
5
13
an expression. Each block conceptually introduces a new namespace scope. Use
@@ -28,7 +36,27 @@ if really needed.
28
36
29
37
## ` unsafe ` blocks
30
38
39
+ > ** <sup >Syntax</sup >**
40
+ > _ UnsafeBlockExpression_ :
41
+ >   ;  ; ` unsafe ` _ BlockExpression_
42
+
31
43
_ See [ ` unsafe ` block] ( unsafe-blocks.html ) for more information on when to use ` unsafe ` _
32
44
33
45
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
You can’t perform that action at this time.
0 commit comments