Skip to content

Commit 5cdf128

Browse files
committed
Document foreign variadic functions in TRPL and the reference.
Fixes #38485.
1 parent f536d90 commit 5cdf128

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/doc/book/ffi.md

+25
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,31 @@ The [`libc` crate on crates.io][libc] includes type aliases and function
574574
definitions for the C standard library in the `libc` module, and Rust links
575575
against `libc` and `libm` by default.
576576

577+
# Variadic functions
578+
579+
In C, functions can be 'variadic', meaning they accept a variable number of arguments. This can
580+
be achieved in Rust by specifying `...` within the argument list of a foreign function declaration:
581+
582+
```no_run
583+
extern {
584+
fn foo(x: i32, ...);
585+
}
586+
587+
fn main() {
588+
unsafe {
589+
foo(10, 20, 30, 40, 50);
590+
}
591+
}
592+
```
593+
594+
Normal Rust functions can *not* be variadic:
595+
596+
```ignore
597+
// This will not compile
598+
599+
fn foo(x: i32, ...) { }
600+
```
601+
577602
# The "nullable pointer optimization"
578603

579604
Certain Rust types are defined to never be `null`. This includes references (`&T`,

src/doc/reference.md

+9
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,15 @@ Functions within external blocks may be called by Rust code, just like
16571657
functions defined in Rust. The Rust compiler automatically translates between
16581658
the Rust ABI and the foreign ABI.
16591659

1660+
Functions within external blocks may be variadic by specifying `...` after one
1661+
or more named arguments in the argument list:
1662+
1663+
```ignore
1664+
extern {
1665+
fn foo(x: i32, ...);
1666+
}
1667+
```
1668+
16601669
A number of [attributes](#ffi-attributes) control the behavior of external blocks.
16611670

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

0 commit comments

Comments
 (0)