Static rglua::lua::lua_next[][src]

pub static lua_next: Lazy<extern "C" fn(l: LuaState, idx: c_int) -> c_int>
Expand description

Pops a key from the stack, and pushes a key-value pair from the table at the given index (the “next” pair after the given key). If there are no more elements in the table, then lua_next returns 0 (and pushes nothing).

Safety

Do not call lua_tolstring on a string while traversing a table. This will confuse next since it modifies the key.

Examples

use rglua::prelude::*;
#[lua_function]
fn table_traverse(l: LuaState) -> i32 {
    // Assume a table is in the stack at index 1 (first argument of this function)
    lua_pushnil(l);  // first key
    // This is nil as how ``pairs()`` passes nil to ``next()`` in lua.
    while lua_next(l, 1) != 0 {
        // Uses 'key' (at index -2) and 'value' (at index -1)
        println!("{} - {}", rstr!(luaL_typename(l, -2)), rstr!(luaL_typename(l, -1)));
        // Removes 'value'; keeps 'key' for next iteration
        lua_pop(l, 1);
    }
    0
}