TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Crabtime: Zig’s Comptime in Rust

466 点作者 klaussilveira大约 2 个月前

16 条评论

weinzierl大约 2 个月前
I love the logo, it is brilliant.<p>Making Rust&#x27;s macros easier is laudable. Purely from a user&#x27;s perspective I find it especially annoying, that proc macros need their own crate, even if I understand the reasons for it. If I read Crabtime correctly it solves that problem, which is nice.<p>That being said Crabtime looks more like compile time eval on steroids to me than an analogon to Zig&#x27;s comptime.<p>One (maybe <i>the</i>) distinguishing feature between comptime in Zig and Rust macros seems to me to be access to type information. In Zig you have it[1] in Rust you don&#x27;t and that makes a big difference. It doesn&#x27;t look like we will get that in Rust anytime soon and the projects that need it (e.g. cargo semver check) use dirty tricks (parsing RustDoc from the macro) to accomplish it. I did not see anything like that in Crabtime, but I might have missed it. At any rate, I&#x27;d expect compile time reflection for anything that claims to bring comptime to Rust.<p>[1] I think, but I am not a Zig expert, so please correct me if I am wrong.
评论 #43445526 未加载
评论 #43444472 未加载
评论 #43444067 未加载
nindalf大约 2 个月前
I tried the library out and it worked pretty well for me.<p>I had previously written a declarative macro to generate benchmark functions [1]. It worked, but I didn&#x27;t enjoy the process of getting it working. Nor did I feel confident about making changes to it.<p>When I rewrote it using crabtime I found the experience much better. I was mostly writing Rust code now, something I was familiar with. The code is much more readable and customisable [2]. For example, instead of having to pass in the names of the modules each time I added a new one, I simply read the files from disk at compile time.<p>To compare the two see what the code looks like in within the braces of paste!{} in the first one and crabtime::output!{} in the second one. The main difference is that I can <i>construct</i> the strings using Rust code and drop them in with a simple {{ str }}. With paste!, I don&#x27;t know exactly what I did, but I kept messing around until it worked.<p>Or compare the two loops. In the first one we have `($($year:ident {$($day:ident),+ $(,)?}),+ $(,)?)` while with crabtime we have plain Rust code - `for (year, day) in years_and_days`. I find the latter more readable.<p>Overall I&#x27;m quite pleased with crabtime. Earlier I&#x27;d avoid Rust metaprogramming as much as possible, but now I&#x27;d be open to writing a macro if the situation called for it.<p>[1] - <a href="https:&#x2F;&#x2F;github.com&#x2F;nindalf&#x2F;advent&#x2F;blob&#x2F;13ff13&#x2F;benches&#x2F;benches&#x2F;bench.rs#L7" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nindalf&#x2F;advent&#x2F;blob&#x2F;13ff13&#x2F;benches&#x2F;benche...</a><p>[2] - <a href="https:&#x2F;&#x2F;github.com&#x2F;nindalf&#x2F;advent&#x2F;blob&#x2F;b72b98&#x2F;benches&#x2F;benches&#x2F;bench.rs#L52" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nindalf&#x2F;advent&#x2F;blob&#x2F;b72b98&#x2F;benches&#x2F;benche...</a>
stared大约 2 个月前
I love these kinds of acknowledgements, as they not only show gratitude, but also give a glimpse into the collaborative, creative process:<p>&gt; We would like to extend our heartfelt gratitude to the following individuals for their valuable contributions to this project:<p>&gt; timonv – For discovering and suggesting the brilliant name for this crate. Read more about it here (<a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;1j42fgi&#x2F;comment&#x2F;mg6pwfe&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;1j42fgi&#x2F;comment&#x2F;mg6pw...</a>).<p>&gt; Polanas – For their invaluable assistance with testing, design, and insightful feedback that greatly improved the project.<p>&gt; Your support and contributions have played a vital role in making this crate better—thank you!
mplanchard大约 2 个月前
At first I was like wait this looks just like eval_macro, which I discovered a couple of weeks ago. Looks like it is just renamed! The new name is great, congrats on the improved branding :)
评论 #43446228 未加载
vlovich123大约 2 个月前
Wow this is so neat. Has anyone had any experience with it &#x2F; feedback? This looks so much nicer than existing macros.
评论 #43444284 未加载
评论 #43444464 未加载
评论 #43444027 未加载
norman784大约 2 个月前
This looks nice, just yesterday I was trying to make my code more concise by using some macro_rules magic, but it was a bit more than what macro_rules can handle, so I ended up just writing the whole thing. I avoid whenever I can proc macros, I wrote my fair share of macros, but I hate them, you need to add most of the time 3 new dependencies, syn, quote and proc_macros2, that adds up to the compilation times.<p>This looks worth the playing with and see if they can solve my issue, one thing I avoid as much as possible is to add unnecessary dependencies, didn’t check how many dependencies this will add overall to the project.
评论 #43444494 未加载
jpgvm大约 2 个月前
This is cursed in the most wonderful way, kudos.
评论 #43445256 未加载
dymk大约 2 个月前
This looks cool, but how it impacts project compile times? They talk about how caching works for multiple invocations of the same macro with different arguments. It would be nice to have some approximate numbers for how long it takes to create, compile, and execute one of its generated projects.
codedokode大约 2 个月前
The problem with macros in Rust is that they have full access to your computer. This is literally an invitation for exploitation. I think we will see the attacks based on this vulnerability once Rust becomes more popular.
评论 #43448179 未加载
评论 #43448732 未加载
评论 #43447882 未加载
评论 #43451005 未加载
the__alchemist大约 2 个月前
Deos anyone have an example beyond the one on that page? I&#x27;m having a hard time understanding.<p>So, I&#x27;m interested in some metaprogramming right now. I&#x27;m setting up Vec3 SIMD types, and it requires a lot of repetition to manage the various variants: f32::Vec3x8, f64::Vec3x16 etc that are all similar internally. This could be handled using traditional macros, procedural macros, or something called &quot;code gen&quot;, which I think is string manipulation of code. Could I use crabtime to do this instead? Should I?
评论 #43445588 未加载
评论 #43448025 未加载
KolmogorovComp大约 2 个月前
A better link <a href="https:&#x2F;&#x2F;docs.rs&#x2F;crabtime&#x2F;1.1.1&#x2F;crabtime&#x2F;" rel="nofollow">https:&#x2F;&#x2F;docs.rs&#x2F;crabtime&#x2F;1.1.1&#x2F;crabtime&#x2F;</a>
jgalt212大约 2 个月前
Does anyone else find macros make it hard to grep a code base? This does seem like something semantic grep could solve, but I&#x27;m unaware of any semantic grep macros use cases.
评论 #43445961 未加载
评论 #43448055 未加载
评论 #43445986 未加载
cchance大约 2 个月前
I don&#x27;t know if i&#x27;m too dumb i never understood what comptime gives, i get what macros are for but how does something like crabtime improve things ?<p>Does this basically allow us to write normal rust code instead of procmacros, with even fewer constraints?
评论 #43450933 未加载
jedisct1大约 2 个月前
It&#x27;s nothing like Zig&#x27;s comptime.
cyber1大约 2 个月前
No, this is not Zig comptime at all. Zig&#x27;s comptime work is on another level, and it&#x27;s amazing.
评论 #43448415 未加载
tdhz77大约 2 个月前
Moments like this realize I don’t understand programming fundamentals. Imposter syndrome sits in and I realize that my lack of formal education is costing me.