Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- println!("{}", note("C", "Do"));
- println!("{}", note("C", "Re"));
- println!("{}", note("C", "Mi"));
- println!("{}", note("D", "Mi"));
- println!("{}", note("A#", "Fa"));
- }
- fn note(scale: &str, name: &str) -> String {
- let notes = vec!["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
- struct Name {
- label: &'static str,
- position: usize
- }
- let names = vec![
- Name{label: "Do", position: 0},
- Name{label: "Re", position: 2},
- Name{label: "Mi", position: 4},
- Name{label: "Fa", position: 5},
- Name{label: "So", position: 7},
- Name{label: "La", position: 9},
- Name{label: "Ti", position: 11}
- ];
- let scale_position = notes.iter().position(|x| *x == scale).unwrap();
- let name_position = names.iter().find(|x| x.label == name).unwrap().position;
- let mut note_poistion = scale_position + name_position;
- if note_poistion >= notes.len() {
- note_poistion -= notes.len();
- };
- return notes[note_poistion].to_string()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement