Does Rust directly support a rotate operation?


#1

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.


#2

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


#3

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

rotate_value:
.Ltmp0:
.seh_proc rotate_value
.Ltmp1:
	.seh_endprologue
	movl	%ecx, %eax
	movb	%dl, %cl
	roll	%cl, %eax
	retq
	.seh_handlerdata

#4

Daniel,

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.

Ed


#5

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


#6

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


#7

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


#8

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 neil.mohr@futurenet.com

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.


#9

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


#10

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.