A Rust macro implementing a C-style for
loop. See
the docs for more information.
huonw / cfor Goto Github PK
View Code? Open in Web Editor NEWA C-style for loop macro
Home Page: http://huonw.github.io/cfor/cfor/
License: Apache License 2.0
A C-style for loop macro
Home Page: http://huonw.github.io/cfor/cfor/
License: Apache License 2.0
A Rust macro implementing a C-style for
loop. See
the docs for more information.
The following code does not compile:
#![feature(phase)]
#[phase(plugin)] extern crate cfor;
fn main() {
let d = 2;
cfor!{let mut x = 1u; x < 0x1000; x *= d {
println!("power of 2: {}", x);
}}
}
and results in
/home/tom/tmp/cfor-bug/src/main.rs:7:16: 7:17 error: expected `:` but found `!`
/home/tom/tmp/cfor-bug/src/main.rs:7 println!("power of 2: {}", x);
^
The following version works
#![feature(phase)]
#[phase(plugin)] extern crate cfor;
fn main() {
let d = 2;
cfor!{let mut x = 1u; x < 0x1000; (x *= d) {
println!("power of 2: {}", x);
}}
}
In C, you can use multiple initialization and steps separated by commas like so:
for (int i = 0, j = 5, i < 10; i++, j+=10) {
printf("%d, %d\n", i, j);
}
Implementing this in the main use case is trivial (changed lines are marked in comments)
($($init: stmt),+; $cond: expr; $($step: expr),+; $body: block) => { // Changed
{
$($init;)+ // Changed
while $cond {
let mut _first = true;
let mut _continue = false;
loop {
if !_first { _continue = true; break }
_first = false;
$body
}
if !_continue {
break
}
$($step;)+ // Changed
}
}
};
However, implementing this in your corner-case macros has, thus far stumped me.
Hi, currently we have to type this:
#[macro_use] extern crate cfor;
fn main() {
cfor!{let mut x = 1; x < 0x1000; x *= 2; {
println!("power of 2: {}", x);
}}
}
But to make it closer to c-style for loop, wouldn't this be better:
#[macro_use] extern crate cfor;
fn main() {
cfor! let mut x = 1; x < 0x1000; x *= 2; {
println!("power of 2: {}", x);
}
}
I'm looking to vendor a copy of this so it would be nice if the crate could be republished with extra files dropped.
The extra files are:
a.out
test.c
tests/test2.rs_
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.