Does Rust directly support a rotate operation?

Almost all CPUs provide a rotate operation. The C language has shift but not rotate. Some C compilers support a rotate intrinsic but it would be nice if Rust directly offered a rotate operation in all the various operand sizes. I have often wanted a rotate operation when coding C and am working on an encryption system now that would benefit from a rotate operation that was as fast as the assembly language operation.

1 Like

All integer types have rotate_left and rotate_right methods. Search for it in the docs.


Also worth to remember that if you have some code like this

pub fn rotate_value(value: u32, shift: u32) -> u32 {
	(value << shift) | (value >> (32 - shift))

The compiler will actually generate a roll instruction

.seh_proc rotate_value
	movl	%ecx, %eax
	movb	%dl, %cl
	roll	%cl, %eax


Thanks for your reply. I will go look. The more I learn about Rust the more
I am finding that it actually could be my replacement for C, which despite
initial interest in C++ and others, they never were.


This is one of the many good design decisions of Rust.

I am realizing that. Linux Format magazine (Brit) is doing tutorial
articles on Rust for anyone interested.

The articles aren't visible for free, but the code is. And the code doesn't look that good :frowning:

Since I have NO expertise in coding Rust it would be best for you to
contact the author if you have concerns about his code. The author’s name
is Mihalis Tsoukalos and you could contact him through Linux Format’s
editor Neil Mohr at

It would be useful to see tutorials like this series in computer magazines
if Rust will achieve a strong position, so I would urge you to make your
concerns known in a constructive way.

You're right. That is awesome. Somebody on that side of the pond should buy Mihalis a beer!

Since you know Rust so well why not write your own series on it by
contacting the editor? Not that much has been published and tutorials would
be useful to move Rust along.

1 Like