我已经尝试了多种方法,但这是一个猜谜游戏
fn main() {
let _array: [&'static str; 4] = ["First", "Second", "Third", "Fourth"];
check_string(/*_array*/);
}
fn check_string(/*_input: ??? */) {
}
最佳答案
检查rust中变量类型的一种好方法是执行以下操作:
let val: () = /* your value */;
(Playground)
这使您出错,说它期望使用
()
而不是WhatEverYourTypeIs
在这种情况下,您将获得一个[&'static str; 4]
,正如您在代码中已经提到的那样。所以只需要在函数中:fn check_string(data: [&'static str; 4])
您还可以传递一个切片:
fn check_strings(data: &[&'static str])
玩一辈子:
fn check_strings<'a>(data: &'a[&'a str])
等等。
要调用它,请通过以下任一方法:
check_string(_array);
如果需要一个大小合适的数组,或者
check_strings(&_array[..]);
如果需要切片。
关于rust - 如何创建可以作为Rust中的参数传递的字符串数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54394334/