@@ -20,7 +20,6 @@ pub fn transposition(decrypt_mode: bool, msg: &str, key: &str) -> String {
20
20
21
21
for cipher_key in keys. iter ( ) {
22
22
let mut key_order: Vec < usize > = Vec :: new ( ) ;
23
- let mut counter: u8 = 0 ;
24
23
25
24
// Removes any non-alphabet characters from 'msg'
26
25
cipher_msg = cipher_msg
@@ -36,10 +35,9 @@ pub fn transposition(decrypt_mode: bool, msg: &str, key: &str) -> String {
36
35
37
36
key_ascii. sort_by_key ( |& ( _, key) | key) ;
38
37
39
- key_ascii. iter_mut ( ) . for_each ( |( _, key) | {
40
- * key = counter;
41
- counter += 1 ;
42
- } ) ;
38
+ for ( counter, ( _, key) ) in key_ascii. iter_mut ( ) . enumerate ( ) {
39
+ * key = counter as u8 ;
40
+ }
43
41
44
42
key_ascii. sort_by_key ( |& ( index, _) | index) ;
45
43
@@ -91,18 +89,16 @@ fn encrypt(mut msg: String, key_order: Vec<usize>) -> String {
91
89
// alphabetical order of the keyword's characters
92
90
let mut indexed_vec: Vec < ( usize , & String ) > = Vec :: new ( ) ;
93
91
let mut indexed_msg: String = String :: from ( "" ) ;
94
- let mut counter: usize = 0 ;
95
92
96
- key_order. into_iter ( ) . for_each ( |key_index| {
93
+ for ( counter , key_index ) in key_order. into_iter ( ) . enumerate ( ) {
97
94
indexed_vec. push ( ( key_index, & encrypted_vec[ counter] ) ) ;
98
- counter += 1 ;
99
- } ) ;
95
+ }
100
96
101
97
indexed_vec. sort ( ) ;
102
98
103
- indexed_vec . into_iter ( ) . for_each ( | ( _, column) | {
99
+ for ( _, column) in indexed_vec {
104
100
indexed_msg. push_str ( column) ;
105
- } ) ;
101
+ }
106
102
107
103
// Split the message by a space every nth character, determined by
108
104
// 'message length divided by keyword length' to the next highest integer.
@@ -153,19 +149,19 @@ fn decrypt(mut msg: String, key_order: Vec<usize>) -> String {
153
149
msg. replace_range ( range, "" ) ;
154
150
} ) ;
155
151
156
- split_small. iter_mut ( ) . for_each ( |key_index| {
152
+ for key_index in split_small. iter_mut ( ) {
157
153
let ( slice, rest_of_msg) = msg. split_at ( split_size) ;
158
154
indexed_vec. push ( ( * key_index, ( slice. to_string ( ) ) ) ) ;
159
155
msg = rest_of_msg. to_string ( ) ;
160
- } ) ;
156
+ }
161
157
162
158
indexed_vec. sort ( ) ;
163
159
164
- key_order . into_iter ( ) . for_each ( | key| {
160
+ for key in key_order {
165
161
if let Some ( ( _, column) ) = indexed_vec. iter ( ) . find ( |( key_index, _) | key_index == & key) {
166
162
decrypted_vec. push ( column. to_string ( ) ) ;
167
163
}
168
- } ) ;
164
+ }
169
165
170
166
// Concatenate the columns into a string, determined by the
171
167
// alphabetical order of the keyword's characters
0 commit comments