Skip to main content

to_snake_case

Function to_snake_case 

Source
pub fn to_snake_case(s: &str) -> String
Expand description

Convert string to snake_case.

Converts camelCase, PascalCase, and other formats to snake_case by inserting underscores before uppercase letters and converting them to lowercase.

Edge cases:

  • Consecutive uppercase letters (acronyms) like “HTTPServer” → “http_server
  • Leading/trailing underscores are preserved
  • Already snake_case strings pass through unchanged

§Examples

use spikard_cli::codegen::common::case_conversion::to_snake_case;
assert_eq!(to_snake_case("user"), "user");
assert_eq!(to_snake_case("getUser"), "get_user");
assert_eq!(to_snake_case("createUserProfile"), "create_user_profile");
assert_eq!(to_snake_case("HTTPServer"), "http_server");
assert_eq!(to_snake_case("GraphQLType"), "graph_ql_type"); // Splits on each uppercase
assert_eq!(to_snake_case("_id"), "_id");
assert_eq!(to_snake_case("id_"), "id_");