更好的做法是返回一个Option还是只返回一个Vec?

huangapple go评论77阅读模式
英文:

Is it better to return an Option of a Vec, or just a Vec?

问题

Suppose I am writing a function that takes a bunch of strings and filters out the "bad" ones.

The function then returns some strings, but there is a chance that all the strings are filtered out.

So I am wondering, should I use the Option type here? I could see it making sense that the function either returns the None variant when all strings are "bad," and it returns the Some variant when the strings are "good."

The function signature could look something like this:

pub fn filter_strings(inputs: Vec<String>) -> Option<Vec<String>> {
    // Todo...
}

But is there really any point in making this an Option?

Would this just make things unnecessarily more complicated since the consumer kind of needs to now check for the None variant and also for the case where it returns Some empty vector?

Is it simpler and more "idiomatic Rust" to just return a vector of strings here?

pub fn filter_strings(inputs: Vec<String>) -> Vec<String> {
    // Todo...
}

Or is there some advantage to using Option here that I am missing?

英文:

Suppose I am writing a function that takes a bunch of string and filters out the "bad" ones.

The function then returns some strings, but there is a chance that all the strings are filtered out.

So I am wondering, should I use to Option here? I could see it making sense that the function either returns the None option variant when all strings are "bad", and it returns the Some variant when the strings are "good".

The function signature could look something like this:

pub fn filter_strings(inputs: Vec&lt;String&gt;) -&gt; Option&lt;Vec&lt;String&gt;&gt; {
    // Todo...
}

But is there really any point in making this an Option?

Would this just make things unnecessarily more complicated since the consumer kind of needs to now check for the None variant and also for the case where it returns Some empty vector?

Is it simpler and more "idiomatic Rust" to just return a vector of strings here?

pub fn filter_strings(inputs: Vec&lt;String&gt;) -&gt; Vec&lt;String&gt; {
    // Todo...
}

Or is there some advantage to using Option here that I am missing?

答案1

得分: 10

如果你在问是否可以毫无意义地用None替换返回空向量,请不要这样做 - 只需删除Option并返回一个空向量。这样使用返回值的代码会变得更容易 - 当没有理由时,它们不必特别处理空向量。空向量不会从堆中分配任何内存,所以甚至没有性能原因。

然而,在某些情况下,区分可能是有用的。None 可能代表 "未知",而 Some(vec![]) 可能代表 "已知为空"。在设计软件时,上下文很重要。

英文:

If you're asking if it's acceptable to mindlessly replace returning an empty vector with None, don't - just remove the Option and return an empty vector. This makes the code that is using the return value a lot easier - they don't have to handle the empty vector specially when there is no reason to. Empty vectors do not allocate any memory from the heap, so there's not even a performance reason.

However, there may be cases where the distinction is useful. None may represent "unknown", while Some(vec![]) may represent "known to be empty". Context is important when designing software.

答案2

得分: 1

根据上下文,可能会有所帮助使用 Option<Vec>

  • 遵循某种样式或约定(例如已经有一组返回 Option<_> 的接口函数);
  • 便于传递给接受 Option 的其他接口;
  • 适用于某些泛型的 Option<T>

在其他情况下,我更倾向于不使用 Option 返回,因为它更简单,而且在大多数情况下,你想要 None 时可以使用空的 Vec 代替。

英文:

Depending on the context, it may help to use Option&lt;Vec&gt;:

  • to follow a style or convention (for example there are already a group of interface functions returning Option&lt;_&gt;);
  • for the convenience to pass to other interfaces accepting Options;
  • to suit for some generics of Option&lt;T&gt;;

In other cases, I prefer to return without Option as it's simpler, and in most cases you want None you can use empty Vec instead.

huangapple
  • 本文由 发表于 2023年7月18日 09:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定