Add notes on Trait Extensione & extern keyword

This commit is contained in:
Marcello 2021-11-20 09:34:23 +01:00
parent ea59857f4b
commit 66adbca728

View file

@ -662,6 +662,25 @@ fn generic_method<T, U>()
fn generic_return() -> impl RequiredTrait { }
```
### Trait Extensions
```rs
extern crate foo;
use foo::Foo;
// define the methods to be added to the existing type
trait FooExt {
fn bar(&self);
}
// implement the extension methods
impl FooExt for Foo {
fn bar(&self) { .. }
}
```
**NOTE**: the calling code needs to import your new trait in addition to the external type
## Lifetimes
Lifetime annotation indicates to the *borrow checker* that the lifetime of the returned value is as long as the lifetime of the referenced value.
@ -889,3 +908,34 @@ fs::write("path/to/file", contents);
let mut file = fs::OpenOptions::new().append(true).open("path/to/file").unwrap();
file.write(b"appended text"); // write wants an [u8]
```
## External Code
The extern keyword is used in two places in Rust:
- in conjunction with the crate keyword to make Rust code aware of other Rust crates in the project
- in foreign function interfaces (FFI).
`extern` is used in two different contexts within FFI. The first is in the form of external blocks, for declaring function interfaces that Rust code can call foreign code by.
```rs
#[link(name = "my_c_library")]
extern "C" {
fn my_c_function(x: i32) -> bool;
}
```
This code would attempt to link with `libmy_c_library.so` on unix-like systems and `my_c_library.dll` on Windows at runtime, and panic if it can't find something to link to.
Rust code could then use my_c_function as if it were any other unsafe Rust function.
Working with non-Rust languages and FFI is inherently unsafe, so wrappers are usually built around C APIs.
The mirror use case of FFI is also done via the extern keyword:
```rs
#[no_mangle]
pub extern "C" fn callable_from_c(x: i32) -> bool {
x % 3 == 0
}
```
If compiled as a dylib, the resulting `.so` could then be linked to from a C library, and the function could be used as if it was from any other library