System calls, FFI, and cross-platform abstractions 57 extern "C" (sometimes written without the "C", since "C" is assumed if nothing is specified) means we want to use the "C" calling convention when calling the function write in the "C" library we're linking to. This function needs to have the exact same name as the function in the library we're linking to. The parameters don't have to have the same name, but they must be in the same order. It's good practice to name them the same as in the library you're linking to. Here, we use Rusts FFI, so when you read about using FFI to call external functions, it's exactly what we're doing here. The write function takes a file descriptor, fd, which in this case is a handle to stdout. In addition, it expects us to provide a pointer to an array of u8, buf values and the length of that buffer, count. Calling convention This is the first time we've encountered this term, so I'll go over a brief explanation, even though we dive deeper into this topic later in the book. A calling convention defines how function calls are performed and will, amongst other things, specify:-How arguments are passed into the function-What registers the function is expected to store at the start and restore before returning-How the function returns its result-How the stack is set up (we'll get back to this one later) So, before you call a foreign function you need to specify what calling convention to use since there is no way for the compiler to know if we don't tell it. The C calling convention is by far the most common one to encounter. Next, we wrap the call to our linked function in a normal Rust function. ch03/b-normal-syscall #[cfg(target_family = "unix")] fn syscall(message: String)-> io::Result<()> { let msg_ptr = message. as_ptr(); let len = message. len(); let res = unsafe { write(1, msg_ptr, len) }; if res ==-1 { return Err(io::Error::last_os_error()); } Ok(()) }