本文介绍了我如何定义一个返回自己类型的Rust函数类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在学习Rust,并且仍然非常努力想要解决它。考虑以下Go定义: $ b type FnType func(paramType)FnType code> 它只是一个返回相同类型函数的函数。 Rust中可以实现类似的东西吗?而且,理想情况下,是否可以一般地完成,以便客户端指定 paramType ?解决方案我在文档中进行了一些挖掘工作,并将它们带到了操场上,我认为我自己能够回答这个问题,尽管它需要一个中间类型: enum fn main(){ let mut state = State :: code>一些(第一); ,同时让State :: Some(s)= state { state = s(0)} } enum State< T> {一些(fn(T) - > State< T>), None,} fn first(_:i32) - >状态< I32> { println!(First); State :: Some(second)} fn second(_:i32) - >状态< I32> { println!(Second); State :: None } 您可以验证它是否在游乐场。 I'm learning Rust, and still very much trying to get my head around it. Consider the following Go definition:type FnType func(paramType) FnTypeIt's just a function that returns a function of the same type. Can something similar be implemented in Rust? And, ideally, can it be done generically, so that paramType is specified by the client? 解决方案 I did some digging in the docs and took to the playground and I think I've been able to answer this myself, although it does require an intermediary type: an enum, to be specific.fn main() { let mut state = State::Some(first); while let State::Some(s) = state { state = s(0) }}enum State<T> { Some(fn(T) -> State<T>), None,}fn first(_: i32) -> State<i32> { println!("First"); State::Some(second)}fn second(_: i32) -> State<i32> { println!("Second"); State::None}You can verify that it runs on the playground. 这篇关于我如何定义一个返回自己类型的Rust函数类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 07:29