Return-type based dispatch

栏目: IT技术 · 发布时间: 4年前

内容简介:One surprising feature of type inference in languages like Rust is defining functions with generic return types. The idea is that by specifying at some later point in the code which type you want your function to return, the compiler can go back and fill i

One surprising feature of type inference in languages like Rust is defining functions with generic return types. The idea is that by specifying at some later point in the code which type you want your function to return, the compiler can go back and fill in the blanks.

For example, let’s have a look at this function:

fn new<T: Default>() -> T {
  T::default()
}

You pick the output

It has no value parameters, but one type parameter, T . That T is its return type and also used in the function body. You can call it like so:

let x: u32 = new();

Or, being explicit about the type parameter, like this:

let x = new::<i32>();

This is quite neat!

More generic: collect

A promising way to be more generic in Rust is to use more traits! Have a look at how the Iterator::collect method is defined:

fn collect<B: FromIterator<Self::Item>>(self) -> B // ...

You can read this type signature as

Consume self and return something of a type that implements can be made From [an] Iterator for the type of items we are iterating over.

Like above, we call this by specifying what kind of output type want. [Looking][ FromIterator implementors] at some of the types FromIterator is implemented for is pretty revealing of the use cases. You can get:

  • a Vec by collecting any items,
  • a BTreeMap or HashMap by collecting tuples,
  • but also PathBuf by collecting Path s,
  • and String for strings and string slices.

Note: All these types are what you might call “container” types.

One more for the road

More generic? More traits.

There is one more gem hidden in FromIterator :

impl<A, E, V> FromIterator<Result<A, E>> for Result<V, E> where
    V: FromIterator<A>, // ...

This means: You can construct a Result containing any type of container of items A by collecting items that are Result s of type A . (The first Err will make the outer Result be an Err .) Here’s an example, see the docs for another one:

let input: Vec<Result<i32, ()>> = vec![Ok(1), Ok(2)];
let output: Result<Vec<i32>, ()> = input.into_iter().collect();

Note: If you like type theory: What we’re building is a Result<<T<A>, E>> by collecting Result<A, E> s and specifying T .


以上所述就是小编给大家介绍的《Return-type based dispatch》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

CSS权威指南(第三版·英文影印版)

CSS权威指南(第三版·英文影印版)

[美] Eric A.Meyer / 东南大学出版社 / 2007-06 / 68.00元

著名的CSS专家Eric A.Meyer他招牌式的智慧和无与伦比的经验引领读者探索了CSS的各个部 分,包括属性、标记、特性和实现。此外,他还就现实应用中的一些问题,例如浏览器的支持和设计方针,发表了看法。你所要知道的就是HTML 4.0的知识,这样就可以创建整洁、易于维护的脚本,以与桌面出版系统同样的优雅和控制能力管理网站布局和分页。你将会学到: 精妙地设计文本风格 用户界面、......一起来看看 《CSS权威指南(第三版·英文影印版)》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器