我可以将参数作为数组传递吗?

编程入门 行业动态 更新时间:2024-10-24 10:27:35
本文介绍了我可以将参数作为数组传递吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

例如,代替

assert_eq!(add(2,3), 5);

有什么方法可以调用类似的东西

is there some way to call something like

let params: [u32; 2] = [2 ,3]; assert_eq!(call!(&add, params), 5);

我会发现此功能对于测试非常有用.例如,如果我想为需要大量参数的函数编写多个测试,则希望能够重用这样的虚拟参数对象:

I would find this functionality very useful for testing. E.g., if I want to write multiple tests for a function that takes a large number of params, I want to be able to reuse a dummy param object like this:

#[cfg(test)] mod tests { const dummy: [u32; 5] = [0, 0, 0, 0, 0]; #[test] fn test_first_param() { let mut params = dummy; params[0] = 1; assert_eq!(call!(&add, params)), 1); } #[test] fn test_second_param() { let mut params = dummy; params[1] = 1; assert_eq!(call!(&add, params)), 1); } }

如果不存在此功能,那么有人会对我制作和发布宏感兴趣吗?

If this functionality does not exist, would anyone be interested in me making and publishing a macro?

推荐答案

该语言中没有任何功能可以枚举数组并将每个元素作为参数传递.您确实需要一个宏.

There's no feature in the language that enumerates over an array and passes each element as an argument. You're correct that you'd need a macro.

更多推荐

我可以将参数作为数组传递吗?

本文发布于:2023-07-31 15:41:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1260015.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!