Advertisement
LemmingAvalanche

Custom Option type leads to error in unrelated function

Jul 3rd, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. //This code gives errors (see end of paste). The error does not occur if the enum Opt<T> is commented out, or if the 'case-values' Some and None are mangled to some other names to (seemingly) avoid conflict with the same members of the Option<T> type from the core library.
  2. //How does this error occur? Why does the map function think that I want to use this custom type? Its name doesn't even conflict with the Option<T> type, but I wouldn't expect that that would be a problem anyway, since I assumed that the type of vector.iter() (I assume that this is where the error is) wouldn't be affected by any shadowing in the current rust file.
  3.  
  4. enum Opt<T> {
  5. Some(T),
  6. None
  7. }
  8.  
  9. fn map<T, U>(vector: &[T], fun: |v: &T| -> U) -> Vec<U> {
  10. let mut acc = Vec::new();
  11. for elem in vector.iter() {
  12. acc.push(fun(elem));
  13. }
  14. return acc;
  15. }
  16.  
  17. fn main() {
  18. }
  19.  
  20. // Errors:
  21.  
  22. $ rustc debug.rs
  23. debug.rs:9:5: 12:11 error: mismatched types: expected `core::option::Option<&T>` but found `Opt<<generic #221>>` (expected enum core::option::Option but found enum Opt)
  24. debug.rs:9 for elem in vector.iter() {
  25. debug.rs:10 acc.push(fun(elem));
  26. debug.rs:11 }
  27. debug.rs:12 return acc;
  28. debug.rs:9:5: 12:11 error: mismatched types: expected `core::option::Option<&T>` but found `Opt<<generic #222>>` (expected enum core::option::Option but found enum Opt)
  29. debug.rs:9 for elem in vector.iter() {
  30. debug.rs:10 acc.push(fun(elem));
  31. debug.rs:11 }
  32. debug.rs:12 return acc;
  33. error: aborting due to 2 previous errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement