Use Python-like range with Rust

Introduction

Here's how to use a Python-like range in Rust.

It can be used as follows.

// for i in range(5):
//     print(i)
for i in range!(5) {
    println!("{}", i);
}

// for i in range(1, 5):
//     print(i)
for i in range!(1, 5) {
    println!("{}", i);
}

// for i in range(1, 5, 2):
//     print(i)
for i in range!(1, 5, 2) {
    println!("{}", i);
}

// for i in range(5, 1, -1):
//     print(i)
for i in range!(5, 1, -1) {
    println!("{}", i);
}

manner

You can implement a Python-like range with the following macros.

macro_rules! range {
    ($stop:expr) => {
        0..$stop
    };
    ($start:expr, $stop:expr) => {
        $start..$stop
    };
    ($start:expr, $stop:expr, -$step:expr) => {
        ($stop + 1..$start + 1).rev().step_by($step)
    };
    ($start:expr, $stop:expr, $step:expr) => {
        ($start..$stop).step_by($step)
    };
}

It basically returns the same iterator as the Python range function.

Things impossible

When specifying a negative step, step_by () can only receive ʻusize, so you need to reverse the iterator with rev () . Therefore, the presence or absence of - is judged by pattern matching, and it is decided whether to perform rev () `. Therefore, if a variable contains a negative number and is given as a step, it cannot be determined as a negative number by pattern matching, resulting in a compile error.

//Compile error
let x = -1;
for i in range!(5, 1, x) {
    println!("{}", i);
}

I would like to know if there is a way to implement this well.

Finally

Rust macros have a lot of freedom, so it's a lot of fun.

Recommended Posts

Use Python-like range with Rust
Use RTX 3090 with PyTorch
Use ansible with cygwin
Use pipdeptree with virtualenv
[Python] Use JSON with Python
Use Mock with pytest
Use Gentelella with django
Use tensorboard with Chainer
Use DynamoDB with Python
Use pip with MSYS2
Use Python 3.8 with Anaconda
Use pyright with Spacemacs
Use python with docker
Use LESS with Django
Use Enums with SQLAlchemy
Use tensorboard with NNabla
Use GPS with Edison
Extend NumPy with Rust
Use Trello API with python
Use shared memory with shared libraries
Use "$ in" operator with mongo-go-driver
Use directional graphs with networkx
Use TensorFlow with Intellij IDEA
Use Twitter API with Python
Use pip with Jupyter Notebook
Use DATE_FORMAT with SQLAlchemy filter
Use TUN / TAP with Python
Use sqlite3 with NAO (Pepper)
Use sqlite load_extensions with Pyramid
Use Windows 10 fonts with WSL
Use chainer with Jetson TK1
Use SSL with Celery + Redis
Use Maxout + CNN with Pylearn2
Use WDC-433SU2M2 with Manjaro Linux
Use OpenBLAS with numpy, scipy
Use subsonic API with python3
Use Sonicwall NetExtener with Systemd
Use prefetch_related conveniently with Django
Use AWS interpreter with Pycharm
Use Bokeh with IPython Notebook
Use MLflow with Databricks ④ --Call model -
Use Rust to move Pocket Miku.
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Use vl53l0x with Raspberry Pi (python)
Use PX-S1UD / PX-Q1UD with Jetson nano
Use the preview feature with aws-cli
How to use virtualenv with PowerShell
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
Use rospy with virtualenv in Python3
Use markdown with jupyter notebook (with shortcut)
Use Python in pyenv with NeoVim
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
How to use FTP with Python
Use Windows 10 speech synthesis with Python
Use curl / jq library with Go
I can't use Japanese with pyperclip
Use camera calibration file with OpenCvSharp4
Use MULTI_ORG function with re: dash
Use OpenCV with Python 3 in Window