我正在玩Rust宏系统,以便了解它的工作原理.我写了一个简单的宏,它接受一个标识符并添加一个数字列表.
macro_rules! do_your_thing {
( $v:ident; $($e:expr),+ ) => {
let mut $v = 0;
$(
$v += $e;
)+
};
}
fn main() {
do_your_thing!(i; 3, 3, 3);
println!("{}", i);
}
Run Code Online (Sandbox Code Playgroud)
如果我运行这个程序,编译器会为'$ v + = $ e'中宏的内部每次重复都抱怨三次'i'是一个未解析的名字.
<anon>:5:13: 5:15 error: unresolved name `i`
<anon>:5 $v += $e;
^
Run Code Online (Sandbox Code Playgroud)
我知道Rust中的宏是卫生的.这就是我使用ident指示符的原因.是否有可能为重复提供额外的句法上下文$(...)+?
UPDATE
在DK.的答案之后,我做了一点挖掘并找到了hygiene该--pretty选项的论据.基本上,它在宏扩展发生后注释语法上下文.跑完之后
rustc -Z unstable-options --pretty expanded,hygiene main.rs
Run Code Online (Sandbox Code Playgroud)
在我的初始程序中,它给了我以下输出
fn main /* 67#0 */() {
let mut i /* 68#4 …Run Code Online (Sandbox Code Playgroud) 我有以下程序。我想知道如何setTimer运作。因此,我编写了一个程序,但无法理解为什么 TimerProc 函数没有被调用。为什么?还需要做什么来触发 setTimer/TimerProc。请帮忙。
#include <windows.h>
#include <stdio.h>
VOID CALLBACK TimerProc(
HWND hwnd, // handle of window for timer messages
UINT uMsg, // WM_TIMER message
UINT idEvent, // timer identifier
DWORD dwTime // current system time
) {
printf("from callback\n");
}
int main(int argc, char *argv[])
{
UINT timerid = SetTimer(NULL,1,1000,TimerProc);/*changed the time from 1 to 1000, but no effect*/
printf("timerid %d\n",timerid);
int i,j;
//delay loop, waiting for the callback function to be called
for(j=0;j<0xffffffff;j++);
/*{
printf("%d\n", j); …Run Code Online (Sandbox Code Playgroud) void mscanf(char *format, ...)
{
scanf(format);
}
int main()
{
int n1, n2;
mscanf("%d%d", &n1, &n2);
printf("%d,%d\n", n1, n2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这有什么不对.?我怎么能在这样的函数中使用scanf?谢谢.
我有两个问题,但它们是相互关联的:
part:a->
Run Code Online (Sandbox Code Playgroud)
我一直试图以相反的顺序显示向量的元素.但没有任何工作.我用过iterotar;
for (it=vec.end(); it!=vec.begin(); --it){
// it is iterator not reverse_iterator.
// do work
}
Run Code Online (Sandbox Code Playgroud)
PS我对迭代器不是很熟悉.我今天第一次使用它们以相反的顺序显示elem.
也尝试过;
for (int i=vec.size(); i!=0; i--){
//display
}
Run Code Online (Sandbox Code Playgroud)
无论我做什么,它总是以与它们存在的顺序相同的顺序显示元素,即不以相反的顺序.
part_b->
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将递归函数的输出直接存储到向量中.像代码是:我知道这不起作用.我试过但只是让你知道我的意思.
#include <iostream>
using namespace std;
#include "vector"
int func(int num);
vector <int> vec;
int main() {
int num=34;
// I know this would not work. But is there any possibilitiy that
// I can store the output in a vector.
vec = binary(num);
// trying to display the vector.
for (int …Run Code Online (Sandbox Code Playgroud)