ChatGPT 为 Rust 过程宏提供支持,该宏在编译时生成代码。
ChatGPT 为 Rust 过程宏提供支持,该宏在编译时生成代码。
auto_impl!{}#[auto_test(...)]在运行之前,获取 ChatGPT API 密钥并将其设置为环境变量 OPENAI_API_KEY。
auto_impl!{}语法:
auto_impl! {
$STR_LIT
$TOKEN_STREAM
}其中 $STR_LIT 是提示字符串常量, $TOKEN_STREAM 是目标代码。
示例:
use gpt_macro::auto_impl;
auto_impl! {
"Return fizz if the number is divisible by 3, buzz if the number is divisible by 5, and fizzbuzz if the number is divisible by both 3 and 5."
fn fizzbuzz(n: u32) -> String {
}
#[test]
fn test_fizzbuzz() {
assert_eq!(fizzbuzz(3), "fizz");
assert_eq!(fizzbuzz(5), "buzz");
assert_eq!(fizzbuzz(15), "fizzbuzz");
assert_eq!(fizzbuzz(1), "1");
}
}如您所见,fizzbuzz()实现是不完整的,因此在没有auto_impl!{}的情况下,构建会失败。该宏会解析给定的提示和目标代码,并在展开宏时要求 ChatGPT 填充代码。它会用从 ChatGPT 响应中提取的代码替换目标。然后,Rust 编译器会继续编译代码。
暂无开放 Issues,或尚未同步最近议题。