Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. fn main() {
  2. println!("{}", note("C", "Do"));
  3. println!("{}", note("C", "Re"));
  4. println!("{}", note("C", "Mi"));
  5. println!("{}", note("D", "Mi"));
  6. println!("{}", note("A#", "Fa"));
  7. }
  8.  
  9. fn note(scale: &str, name: &str) -> String {
  10. let notes = vec!["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
  11.  
  12. struct Name {
  13. label: &'static str,
  14. position: usize
  15. }
  16.  
  17. let names = vec![
  18. Name{label: "Do", position: 0},
  19. Name{label: "Re", position: 2},
  20. Name{label: "Mi", position: 4},
  21. Name{label: "Fa", position: 5},
  22. Name{label: "So", position: 7},
  23. Name{label: "La", position: 9},
  24. Name{label: "Ti", position: 11}
  25. ];
  26.  
  27. let scale_position = notes.iter().position(|x| *x == scale).unwrap();
  28.  
  29. let name_position = names.iter().find(|x| x.label == name).unwrap().position;
  30.  
  31. let mut note_poistion = scale_position + name_position;
  32.  
  33. if note_poistion >= notes.len() {
  34. note_poistion -= notes.len();
  35. };
  36.  
  37. return notes[note_poistion].to_string()
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement