1
- # Interacting with foreign code
1
+ % Rust Foreign Function Interface Tutorial
2
+
3
+ # Introduction
2
4
3
5
One of Rust's aims, as a system programming language, is to
4
6
interoperate well with C code.
@@ -38,7 +40,7 @@ fn main(args: ~[~str]) {
38
40
}
39
41
~~~~
40
42
41
- ## Foreign modules
43
+ # Foreign modules
42
44
43
45
Before we can call ` SHA1 ` , we have to declare it. That is what this
44
46
part of the program is responsible for:
@@ -68,7 +70,7 @@ extern mod something {
68
70
}
69
71
~~~~
70
72
71
- ## Foreign calling conventions
73
+ # Foreign calling conventions
72
74
73
75
Most foreign code will be C code, which usually uses the ` cdecl ` calling
74
76
convention, so that is what Rust uses by default when calling foreign
@@ -88,7 +90,7 @@ The `"abi"` attribute applies to a foreign module (it can not be applied
88
90
to a single function within a module), and must be either ` "cdecl" `
89
91
or ` "stdcall" ` . Other conventions may be defined in the future.
90
92
91
- ## Unsafe pointers
93
+ # Unsafe pointers
92
94
93
95
The foreign ` SHA1 ` function is declared to take three arguments, and
94
96
return a pointer.
@@ -118,7 +120,7 @@ caution—unlike Rust's other pointer types, unsafe pointers are
118
120
completely unmanaged, so they might point at invalid memory, or be
119
121
null pointers.
120
122
121
- ## Unsafe blocks
123
+ # Unsafe blocks
122
124
123
125
The ` sha1 ` function is the most obscure part of the program.
124
126
@@ -159,7 +161,7 @@ unsafe fn kaboom() { ~"I'm harmless!"; }
159
161
This function can only be called from an unsafe block or another
160
162
unsafe function.
161
163
162
- ## Pointer fiddling
164
+ # Pointer fiddling
163
165
164
166
The standard library defines a number of helper functions for dealing
165
167
with unsafe data, casting between types, and generally subverting
@@ -202,7 +204,7 @@ unsafe pointer that was returned by `SHA1`. SHA1 digests are always
202
204
twenty bytes long, so we can pass ` 20u ` for the length of the new
203
205
vector.
204
206
205
- ## Passing structures
207
+ # Passing structures
206
208
207
209
C functions often take pointers to structs as arguments. Since Rust
208
210
structs are binary-compatible with C structs, Rust programs can call
0 commit comments