Why toml to_string() get error ValueAfterTable?

Minimal example with the same problem:

use toml::to_string;
use serde::Serialize;

#[derive(Default, Serialize)]
struct Inner {
    inner_field: i32,
}

#[derive(Default, Serialize)]
struct Outer {
    inner: Inner,
    outer_field: i32,
}

fn main() {
    println!("{:?}", to_string(&Outer::default()));
}

Playground

The error documentation seems to imply that this is the limitation of format - TOML can't have any subtables inside the table, by construction; in other words, all simple fields, like numbers and strings, must be written before the complex types like structs (which are mapped to tables).

This is relatively easy to fix if you control the struct definition - you have to change the order in which fields are serialized. In most cases, that means reordering them in the struct itself: if, in the code above, the Outer struct is instead

#[derive(Default, Serialize)]
struct Outer {
    outer_field: i32,
    inner: Inner,
}

then everything works fine.