Skip to main content

pascal_to_snake

Function pascal_to_snake 

Source
pub fn pascal_to_snake(name: &str) -> String
Expand description

Convert a PascalCase or mixed-case name to snake_case with correct acronym handling.

Use this instead of heck::ToSnakeCase when the input is a PascalCase Rust type or enum variant name — heck inserts an underscore before every uppercase letter, which incorrectly splits acronym-style names like Rdfa into rd_fa.

Rules:

  • A run of consecutive uppercase letters is treated as a single acronym word.
  • If the run is followed by a lowercase letter, the last uppercase char begins the next word (e.g. XMLHttpxml_http).
  • A single uppercase letter followed by lowercase is a normal word start.

Examples:

  • MyTypemy_type
  • Rdfardfa
  • HTMLParserhtml_parser
  • XMLHttpRequestxml_http_request
  • IOErrorio_error
  • URLPathurl_path
  • JSONLDjsonld