Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
This list is filtered with keywords : rust iterate

Idiom #113 Iterate over map entries, ordered by values

Print each key k with its value x from an associative array mymap, in ascending order of x.
Multiple entries may exist for the same value x.

Idiom #286 Iterate over characters of a string

Print a line "Char i is c" for each character c of the string s, where i is the character index of c in s (not the byte index).

Make sure that multi-byte characters are properly handled, and count for a single character.

Idiom #242 Iterate over a set

Call a function f on each element e of a set x.

Idiom #143 Iterate alternatively over two lists

Iterate alternatively over the elements of the lists items1 and items2. For each iteration, print the element.

Explain what happens if items1 and items2 have different size.

Idiom #141 Iterate in sequence over two lists

Iterate in sequence over the elements of the list items1 then items2. For each iteration print the element.

Idiom #112 Iterate over map entries, ordered by keys

Print each key k with its value x from an associative array mymap, in ascending order of k.

Idiom #13 Iterate over map keys and values

Access each key k with its value x from an associative array mymap, and print them.

Traversing each key and each associated object of mymap

Idiom #332 List of the keys of a map

Create the list k containing all the keys of the map m

Idiom #83 Regex with character repetition

Declare the regular expression r matching the strings "http", "htttp", "httttp", etc.

Idiom #257 Traverse list backwards

Print each index i and value x from the list items, from the last down to the first.

Idiom #42 Continue outer loop

Print each item v of list a which is not contained in list b.
For this, write an outer loop to iterate on a and an inner loop to iterate on b.

Control flow jumping back to the beginning of the outermost loop

Idiom #265 Even parity bit

Calculate the parity p of the integer variable i : 0 if it contains an even number of bits set, 1 if it contains an odd number of bits set.

Idiom #316 Count occurrences in a list

Determine the number c of elements in the list x that satisfy the predicate p.

Idiom #284 Create a zeroed list of integers

Create a new list a (or array, or slice) of size n, where all elements are integers initialized with the value 0.

Idiom #280 Filter map

Remove all the elements from the map m that don't satisfy the predicate p.
Keep all the elements that do satisfy p.

Explain if the filtering happens in-place, i.e. if m is reused or if a new map is created.