Skip to content

Commit

Permalink
Document foreign variadic functions in TRPL and the reference.
Browse files Browse the repository at this point in the history
Fixes #38485.
  • Loading branch information
frewsxcv committed Dec 27, 2016
1 parent f536d90 commit 5cdf128
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/doc/book/ffi.md
Expand Up @@ -574,6 +574,31 @@ The [`libc` crate on crates.io][libc] includes type aliases and function
definitions for the C standard library in the `libc` module, and Rust links
against `libc` and `libm` by default.

# Variadic functions

In C, functions can be 'variadic', meaning they accept a variable number of arguments. This can
be achieved in Rust by specifying `...` within the argument list of a foreign function declaration:

```no_run
extern {
fn foo(x: i32, ...);
}
fn main() {
unsafe {
foo(10, 20, 30, 40, 50);
}
}
```

Normal Rust functions can *not* be variadic:

```ignore
// This will not compile
fn foo(x: i32, ...) { }
```

# The "nullable pointer optimization"

Certain Rust types are defined to never be `null`. This includes references (`&T`,
Expand Down
9 changes: 9 additions & 0 deletions src/doc/reference.md
Expand Up @@ -1657,6 +1657,15 @@ Functions within external blocks may be called by Rust code, just like
functions defined in Rust. The Rust compiler automatically translates between
the Rust ABI and the foreign ABI.

Functions within external blocks may be variadic by specifying `...` after one
or more named arguments in the argument list:

```ignore
extern {
fn foo(x: i32, ...);
}
```

A number of [attributes](#ffi-attributes) control the behavior of external blocks.

By default external blocks assume that the library they are calling uses the
Expand Down

0 comments on commit 5cdf128

Please sign in to comment.