Sample code for 30+ languages & platforms
Rust

Implement Preprocessor #include with StringBuilder

Demonstrates how to implement #include with a Chilkat StringBuilder.

Chilkat Rust Downloads

Rust
// First build a string that has a preprocessor include
let sb_src = chilkat::StringBuilder::new();
let _ = sb_src.append("1\r\n2\r\n3\r\n");
let _ = sb_src.append("#include <qa_data/txt/helloWorld.txt>\r\n");
let _ = sb_src.append("4\r\n5\r\n");

println!("{}", sb_src.get_as_string().unwrap_or_default());

// sbSrc contains:
// 	1
// 	2
// 	3
// 	#include <qa_data/txt/helloWorld.txt>
// 	4
// 	5

// The qa_data/txt/helloWorld.txt file contains "Hello World!"

let Ok(file_path) = sb_src.get_after_between("#include", "<", ">") else {
    println!("No #include's found.");
    return;
};

println!("filePath: {}", file_path);

// Load the contents of the filePath
let sb_include_file = chilkat::StringBuilder::new();
let _ = sb_include_file.load_file(&file_path, "utf-8");

// Replace the first occurrence of #include <...> line with the contents of the include file.
let _ = sb_src.replace_all_between("#include", ">", &sb_include_file.get_as_string().unwrap_or_default(), true);

println!("{}", sb_src.get_as_string().unwrap_or_default());

// sbSrce now contains:
// 	1
// 	2
// 	3
// 	Hello World!
// 	4
// 	5