- Rust supports integration with other languages.
You can call other languages from Rust, typically C.
So a Rust application could call into a C library,
that provides some low level capabilities, for example
and you can do it the other way around as well.
Other languages can call into Rust.
So that could be any language potentially.
So to call another language from Rust, like C,
this is what you do.
You declare the external function that you want to call
and then you call the external function in an unsafe block.
Again, because you're calling into another language,
which is beyond the control of Rust.
Rust must assume that the code is unsafe.
You have to label the block to be unsafe
so that the Rust compiler knows not to check it.
So here's an example where I'm going to call
into a couple of C functions from Rust.
So you declare an external block
and you specify the language that you're gonna call into, C.
And in the C library there's this is a Rust version
of the abs function signature.
So in C there's an abs function which takes a 32 bit integer
and returns the 32 bit integer.
It gives it back the absolute value of an integer,
a positive value.
This is the Rust syntax
for an integer function that takes an integer
and returns an integer.
Okay, so it's like a Rustication of the C prototype.
And in C there's also a function called F abs, fabs
which takes a double
that's a 64 bit float and returns a double.
It gives you, you pass in a double
it gives you back the absolute value of that double.
So these functions already exist in the C library.
I've declared them here
and then if I want to call them from Rust,
I have an unsafe block.
And you just call the functions.
It is literally as simple as that.
You pass in an integer
and it'll call into the underlying C library.
We turn it back integer into 32, call the F abs function
passing in a double 64 bit float
get back a 64 bit float with a positive value.
So this is how Rust can call into other languages, like C.
Doing it the other way round
making a Rust function callable in another language.
In order to make a function
in Rust callable in another language
you mark the function as externally visible.
You also make sure
that the function name isn't mangled by Rust.
Rust can mangle names internally,
bit like the way C plus plus does,
you want to disable the name mangling.
So the function name is exactly as you specified
so that the client language, C
knows what the name of the function really is.
So here's an example.
There's an attribute, no mangle
that's a standard Rust attribute, like a compiler directive.
I declare as a public extern visible in the C language.
My function, this is just a regular Rust function.
You can call me from C, you can also call it from Rust.
It's just a regular function actually.
So you can call it from Rust
and you can also call it from C if you want to.
It's kind of been exported.
Right
So time for an example,
usual project less than 15 additional Rust techniques.
We'll have a look at the main
and then we'll see other language,
a demo language integration.
Okay then.
So in main, I'm gonna uncomment this demo
and here it is.
So I've declared some external functions that exist in C.
Those functions exist already.
Abs and fabs.
I'm going to call those functions in my code.
You can only call other language functions
in an unsafe block.
Okay. So down here I have an unsafe block
and I call functions which are unsafe.
I call abs the first one
and then I call fabs the second one.
And for good measure, I've also got my own unsafe function.
All right bring it into view at the bottom.
I've got an unsafe function, which
potentially could be doing something a little bit edgy.
I mean it's not actually pushing the boat out too much
it's just returning the meaning of life,
the universe and everything.
But it's officially unsafe.
Maybe it's doing point arithmetic or something.
So if you have an unsafe function here
then you can only call that function in an unsafe block.
So this unsafe block can call into C language functions
and it can call into my own unsafe functions as well,
if I have any.
Okay, so that's call in from Rust into C.
The other way round,
if you have a Rust function
that you want to be callable from C,
here is such a function, I suppress mangling,
I declare the function as publicly visible
to a C application.
And the name of the function
the code in here is just a regular function.
It really is just regular Rust.
You have to be a bit careful about parameters
because C might find it difficult to pass in complex types.
So when you pass, if you have parameters in here
you might want to restrict yourself
to things like, you know, raw integers for example
rather than anything too ambitious
to make it easier to bind into from another language.
So you know, don't go overboard.
So anyway, this function has been publicly exported.
So I could, if I had a C application
I could call this function from C.
Okay. It is just a regular Rust function though
so I could actually call it from my own Rust code.
And I've done that actually towards the bottom
of my, this is my kind of driver function here.
Towards the bottom you can see
I have actually invoked that function.
Okay. So that's allowable and the function isn't unsafe
it's just a regular Rust function.
So I can call that function in a normal code block
that doesn't need to be marked as unsafe.
Okay. So we'll run the example.
[Typing]
There we are.
That's marvelous.
It's absolutely super.
So here was the call
to the abs function from the C library.
And here's the call to the fabs function in the C library.
And then finally, by the way
I've called my unsafe function, which returns
the meaning of life, the universe and everything.
And then finally the function
which could be called from C,
but can also be called from Rust greetings
from my Rust function.
So that's the end of the lesson and that's also the end
of the kind of theoretical part of the course.
What we're gonna do in the final two lessons
is apply the knowledge that we've got
and the syntax we've learned
and the libraries that we've discovered
and apply those techniques
into real applications as a case study.
So I've got a a work project in the next lesson
that shows how to do file handling,
reading CSV files, data handling
and talking about how to structure a large application.
And then in the final lesson after that
we're going to look at database access
which be quite a nice way to round things off.