varargs
import std::core::varargs; — in the prelude · source
Types
VaArgs
type struct VaArgs {
ptr: void*;
static new(va: void*) -> VaArgs;
as_ptr(&this) -> void*;
![implicit]
next<T>(&this) -> T
where T: VaArg;
}
A function declared with a trailing args... bucket mirrors C's variadic calling convention, which keeps it ABI-compatible for printf-style interop. The compiler emits va_start/va_end around the body and binds args to the raw va_list. VaArgs wraps that pointer so you can read typed values without hand-rolling va_arg:
function logf(fmt: string, args...) -> void {
mut va: VaArgs = VaArgs::new(args);
const count: i32 = va.next(); // T inferred as i32
const name: string = va.next();
}
as_ptr() hands back the raw va_list for forwarding the rest to a C v*-family function such as vfprintf.
Two limits come from C varargs and no wrapper can fix them. It is not count-safe — nothing records how many arguments were passed or their types, so the callee has to know out of band (a format string, a leading count, a sentinel). And the default argument promotions apply: a variadic call promotes i8, i16, and boolean to i32, and f32 to f64. VaArg is therefore implemented only for the promoted set, and va.next::<i8>() is a compile error by design — read it as i32 and narrow.
Properties
| Property | Type |
|---|---|
ptr | void* |
Constructors
static new(va: void*) -> VaArgs;
static new(va: void*) -> VaArgs;Wrap a variadic body's args bucket: VaArgs::new(args). va is the raw va_list pointer the compiler binds for the args... parameter.
Methods
as_ptr(&this) -> void*;
as_ptr(&this) -> void*;The raw va_list pointer, for forwarding the remaining arguments to a C v*-family function (vfprintf, vsnprintf, ...). Equivalent to passing the original args identifier.
![implicit] next<T>(&this) -> T where T: VaArg;
![implicit] next<T>(&this) -> T where T: VaArg;Read the next variadic argument as T (one of the promoted scalar types), advancing the list. ![implicit] lets the type be recovered from the call's expected type, so const n: i64 = va.next(); works; va.next<i64>() is the explicit form.
Trait implementations
implement trait Copy for VaArgs
implement trait Drop for VaArgs
VaArg
type trait VaArg {
static __pluck(p: void*) -> This;
}
The trait next<T> dispatches through. It is implemented for exactly the types that survive C's default argument promotions, which is why va.next::<i8>() is a compile error rather than a silent misread.
Constructors
static __pluck(p: void*) -> This;
static __pluck(p: void*) -> This;Read the next argument of type This from the va_list p points at, advancing it. p is the raw va_list pointer (VaArgs::as_ptr).
Implementors
implement trait VaArg for i32
implement trait VaArg for u32
implement trait VaArg for i64
implement trait VaArg for u64
implement trait VaArg for f64
implement trait VaArg for string
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct VaArgs {
ptr: void*;
}Single-pointer wrapper over the active va_list. ptr is the raw va_list pointer - the args identifier the compiler binds in a variadic body.
VaArgs.ptr: void*static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
static VaArgs::new(va: void*) -> VaArgsWrap a variadic body's args bucket: VaArgs::new(args). va is the raw va_list pointer the compiler binds for the args... parameter.
(parameter) va: void*type struct VaArgs {
ptr: void*;
}Single-pointer wrapper over the active va_list. ptr is the raw va_list pointer - the args identifier the compiler binds in a variadic body.
VaArgs::as_ptr(&this) -> void*The raw va_list pointer, for forwarding the remaining arguments to a C v*-family function (vfprintf, vsnprintf, ...). Equivalent to passing the original args identifier.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
VaArgs::next<T>(&this) -> T
where T: VaArgRead the next variadic argument as T (one of the promoted scalar types), advancing the list. ![implicit] lets the type be recovered from the call's expected type, so const n: i64 = va.next(); works; va.next<i64>() is the explicit form.
type parameter Twhere T: VaArg
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type trait VaArg {
static __pluck(p: void*) -> This;
}A type that can be read as the next variadic argument. Implemented for the C-promoted scalar set only (see the module note on argument promotion).
std::core::varargsfunction name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function logf(fmt: string, args...) -> void(parameter) fmt: stringprimitive stringNUL-terminated raw string (u8*), matching the C ABI. Length-typed text is Str / String.
(parameter) argsfunction name(count: i32, args...)A variadic parameter: the trailing bucket receives any number of further arguments with C's calling convention, so the function stays ABI-compatible with the printf family. Wrap the bucket in VaArgs to read typed values from it.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut va: VaArgsstatic VaArgs::new(va: void*) -> VaArgsWrap a variadic body's args bucket: VaArgs::new(args). va is the raw va_list pointer the compiler binds for the args... parameter.
(parameter) argsconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const count: i32mut va: VaArgsVaArgs::next<T>(&this) -> T
where T: VaArgRead the next variadic argument as T (one of the promoted scalar types), advancing the list. ![implicit] lets the type be recovered from the call's expected type, so const n: i64 = va.next(); works; va.next<i64>() is the explicit form.
const name: stringtype trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait VaArg {
static __pluck(p: void*) -> This;
}A type that can be read as the next variadic argument. Implemented for the C-promoted scalar set only (see the module note on argument promotion).
static VaArg::__pluck(p: void*) -> ThisRead the next argument of type This from the va_list p points at, advancing it. p is the raw va_list pointer (VaArgs::as_ptr).
(parameter) p: void*ThisThe implementing type, inside a trait or implement body. other: &This in a trait method is a reference to whichever type implements it; This::Item names an associated type.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::core::varargs;VaArgs: a safe, typed wrapper over a C-ABI variadic argument list.
module pageVaArgs::as_ptr(&this) -> void*The raw va_list pointer, for forwarding the remaining arguments to a C v*-family function (vfprintf, vsnprintf, ...). Equivalent to passing the original args identifier.
VaArgs.ptr: void*function args() -> Array<String>Program arguments as UTF-8 strings. The first element is the program name. Returns an empty array if the runtime hasn't published argv yet. Panics on allocation failure.
std::envconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const n: i64implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markerimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencestatic VaArg::__pluck(p: void*) -> ThisRead the next argument of type This from the va_list p points at, advancing it. p is the raw va_list pointer (VaArgs::as_ptr).