简体   繁体   English

如何检查 Vec 中的值是否为无?

[英]How to check if a value in a Vec is None?

I would like to use the fact that Vec.last() returns a None if there is nothing at that index but that index still exists.如果该索引处没有任何内容但该索引仍然存在,我想使用Vec.last()返回None的事实。 The problem is that I can't find a way to compare the result to anything inside of a while or if .问题是我找不到将结果与一段while内的任何内容或if进行比较的方法。 I can however compare it if I use match , although that seems less efficient than using a while loop.但是,如果我使用match ,我可以比较它,尽管这似乎比使用while循环效率低。 Is there a way to compare it in an if or a while loop?有没有办法在ifwhile循环中比较它?

This is what I'm trying to do, but the self.particles.last() == None is invalid since you can't compare things to None except None as far as I can tell.这就是我想要做的,但是self.particles.last() == None是无效的,因为据我所知,除了None之外,您无法将事物与None进行比较。

while vec.last() == None {
    num += 1;
}

I would like to use the fact that Vec.last() returns a None if there is nothing at that index but that index still exists.如果该索引处没有任何内容但该索引仍然存在,我想使用Vec.last()返回None的事实。

You seem to be very confused about the behaviour of Vec::last .您似乎对Vec::last的行为感到非常困惑。 It's absolutely not going to return None if "that index still exists" (whatever that means).如果“该索引仍然存在”(无论这意味着什么),它绝对不会返回None It's going to return None if the vec is empty, that's it.如果 vec 为空,它将返回None ,仅此而已。

So your loop is either never going to execute, or will loop forever.所以你的循环要么永远不会执行,要么永远循环。

This is what I'm trying to do, but the self.particles.last() == None is invalid since you can't compare things to None except None as far as I can tell.这就是我想要做的,但是 self.particles.last() == None 是无效的,因为据我所知,除了 None 之外,您无法将事物与 None 进行比较。

 while vec.last() == None { num += 1; }

No?不? The issue would probably be that Option<T> is PartialEq iff T is PartialEq : https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=00f4ea97a0fad1cd74ef3b6ac6b74dc8问题可能是Option<T>PartialEq如果TPartialEqhttps://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=00f4ea97a0fad1cd74ef3b6ac6b74dc8

#[derive(PartialEq, Eq)]
struct Equatable;
struct NonEquatable;
fn main() {
    // compiles fine
    println!("{}", None == Some(Equatable));
    // can't compile
    // println!("{}", None == Some(NonEquatable));
}

Though it is obviously not the normal way to do it, you'd usually use Option::is_none , possibly matches!虽然这显然不是正常的方法,但您通常会使用Option::is_none ,可能matches! in some rare cases (but that's more for enums which don't have predicates).在极少数情况下(但对于没有谓词的枚举来说更是如此)。

You can use pattern matching syntax.您可以使用模式匹配语法。 What is that?那是什么? Well, normally you'd write something like this to loop as long as you get Some values and simultaneously bind the items to a variable:好吧,通常你会写这样的东西来循环,只要你得到Some值并同时将项目绑定到一个变量:

while let Some(elem) = vec.last() {
    ...
}

Some(elem) is a pattern. Some(elem)是一种模式。 It turns out you can replace it with a pattern that has no variable: None .事实证明,您可以将其替换为没有变量的模式: None

while let None = vec.last() {
    ...
}

I admit let without a variable looks odd, and it looks like it's backwards having the None on the left, but it works.我承认let没有变量看起来很奇怪,看起来左边有None是向后的,但它有效。

(Also, I should point out this will be an infinite loop if you don't modify vec , yes?) (另外,我应该指出,如果您不修改vec ,这将是一个无限循环,是吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM