Let's extend things a bit

The extern block can include as many (or as few) of the methods required from the library the Rust application is using.

With each new function added to the extern block, it is always a good idea to test the function being included. This can be done as either unit tests or by adding the function to the extern block and then calling that function from within main.

We can also have multiple Rust source files that include the library functions.

For example, make the changes in the Source1.rs file:

//Source1.rs
[link(name="mylib")] extern { fn some_method(a: f32) → f32; fn some_other_method(a: i32, b: f32, c: f64) → f64; }

Now, make changes in the Source2.rs file:

[link(name="mylib")] 
extern 
 { 
    fn some_other_method(a: i32, b: f32, c: f64) → f64; 
    fn some_text_method() → String; 
} 

As long as the link line is included, this won't cause an issue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset