#Going from python to Rust. How would I do this function?

5 messages · Page 1 of 1 (latest)

worthy epoch
#

Here is the function in python:

list1 = ["one", "two", "three", "four"]
list2 = ["three", "four"]
def tester():
    for item in list1:
        if item not in list2:
            print(f"found! {item}")

Im just sorting through the list and finding the difference between the two. I'm trying to write this in rust for the speed

fn test() {
    let mut list1 = vec!("one", "two", "three", "four");
    let mut list2 = vec!("three", "four");
    for item in list1 {
        if item not in list2 {  // This line is not happy
            println!("{}", item)
        }
    }
}

I tried to do a similar thing In rust. I am wanting it to print out "one" & "two".
The problem that im having is that there seems to be no 'not' operator and 'in <iterable>' seems to not work. Tried looking at the docs and didn't see any mention of a 'not' type operator. Any help would be appreciated

somber tartan
#

thats indeed not a thing, there are no magic keywords like that

#

but Rust does have another handy tool, iterators

#

in this case, we can just use !list2.contains(item)

#

instead of using cursed keywords, we use methods, in this case, check out the methods on Vec<T> (and associated slice &[T])