Consider this Rust example program—it has 4 functions declared at the top. In main, we create a Vec of function pointers.
fn no_action() -> &'static str {
"no action"
}
fn core() -> &'static str {
"core"
}
fn art() -> &'static str {
"art"
}
fn top() -> &'static str {
"top"
}
fn main() {
// Create vector of function pointers.
let mut lookup: Vec<fn() -> &'static str> = vec![];
for _ in 0..128 {
lookup.push(no_action);
}
// Store functions as function pointers at specific indexes.
lookup[b'c' as usize] = core;
lookup[b'a' as usize] = art;
lookup[b't' as usize] = top;
// Loop over this value and call function for each byte.
let value =
"cat";
println!(
"VALUE: {}", value);
for c in value.bytes() {
println!(
"RESULT: {}, {}", c as char, lookup[c as usize]());
}
}
VALUE: cat
RESULT: c, core
RESULT: a, art
RESULT: t, top