Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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.
- //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.
- enum Opt<T> {
- Some(T),
- None
- }
- fn map<T, U>(vector: &[T], fun: |v: &T| -> U) -> Vec<U> {
- let mut acc = Vec::new();
- for elem in vector.iter() {
- acc.push(fun(elem));
- }
- return acc;
- }
- fn main() {
- }
- // Errors:
- $ rustc debug.rs
- 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)
- debug.rs:9 for elem in vector.iter() {
- debug.rs:10 acc.push(fun(elem));
- debug.rs:11 }
- debug.rs:12 return acc;
- 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)
- debug.rs:9 for elem in vector.iter() {
- debug.rs:10 acc.push(fun(elem));
- debug.rs:11 }
- debug.rs:12 return acc;
- error: aborting due to 2 previous errors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement