Strings and Iterators
In this exercise, you are implementing a routing component of a web server. The server is configured with a number of path prefixes which are matched against request paths. The path prefixes can contain a wildcard character which matches a full segment. See the unit tests below.
Copy the following code to <https://play.rust-lang.org/> and make the tests pass. Try avoiding allocating a Vec for your intermediate results:
1. #![allow(unused)]
2. fn main() {
3. // TODO: remove this when you're done with your implementation.
4. #![allow(unused_variables, dead_code)]
6. pub fn prefix_matches(prefix: &str, request_path: &str) -> bool {
7. unimplemented!()
8. }
10. #[test]
11. fn test_matches_without_wildcard() {
12. assert!(prefix_matches("/v1/publishers", "/v1/publishers"));
13. assert!(prefix_matches("/v1/publishers", "/v1/publishers/abc-123"));
14. assert!(prefix_matches("/v1/publishers", "/v1/publishers/abc/books"));
16. assert!(!prefix_matches("/v1/publishers", "/v1"));
17. assert!(!prefix_matches("/v1/publishers", "/v1/publishersBooks"));
18. assert!(!prefix_matches("/v1/publishers", "/v1/parent/publishers"));
19. }
21. #[test]
22. fn test_matches_with_wildcard() {
23. assert!(prefix_matches(
24. "/v1/publishers/*/books",
25. "/v1/publishers/foo/books"
26. ));
27. assert!(prefix_matches(
28. "/v1/publishers/*/books",
29. "/v1/publishers/bar/books"
30. ));
31. assert!(prefix_matches(
32. "/v1/publishers/*/books",
33. "/v1/publishers/foo/books/book1"
34. ));
36. assert!(!prefix_matches("/v1/publishers/*/books", "/v1/publishers"));
37. assert!(!prefix_matches(
38. "/v1/publishers/*/books",
39. "/v1/publishers/foo/booksByAuthor"
40. ));
41. }
42. }
