获取作为参数传入的数组的大小

时间:2021-12-09 21:37:13

I can't seem to make this work. I keep getting an error saying 'len' doesn't exist on type '&[str]'.

我似乎做不到。我不断得到一个错误,说'len'在type '&[str]'上不存在。

fn testLength(arr: &[str]) {
    if arr.len >= 10 {
        // Do stuff
    }
}

I'm still pretty new to Rust, and I understand this is a pointer to a raw string somewhere. Why can't I get the length of the underlying string at runtime? Googling things like "length of string in rust" and "length of array in rust" lead me absolutely no where.

我还是个新手,我知道这是一个指向原始字符串的指针。为什么我不能在运行时获得底层字符串的长度?在谷歌上搜索“铁丝的长度”和“铁丝的长度”,绝对不会让我有任何收获。

1 个解决方案

#1


19  

Of course, you can do it - it's just len is not a field, it's a method:

当然,你可以这么做——只是len不是一个字段,它是一个方法:

fn test_length(arr: &[String]){
    if arr.len() >= 10 {
        // Do stuff
    }
}

If you only started learning Rust, you should read through the official book - you will also find why &[str] does not make sense (in short, str is unsized type, and you can't make an array of it; instead &str should be used for borrowed strings and String for owned strings; most likely you have a Vec<String> somewhere, and you can easily get &[String] out of it).

如果你只是开始学习铁锈,你应该通读这本书——你也会发现为什么&[str]没有意义(简而言之,str是未大小的类型,你不能对它进行数组;相反,&str应该用于借来的字符串,而用于拥有的字符串;很可能您在某个地方有一个Vec ,您可以很容易地从中获得&[String])。

I would also add that it is not clear if you want to pass a string or an array of strings into the function. If it is a string, then you should write

我还要补充一点,如果您想要将字符串或字符串数组传递到函数中,还不清楚。如果它是一个字符串,那么你应该写

fn test_length(arr: &str) {
    if arr.len() >= 10 {
        // Do stuff
    }
}

len() on a string, however, returns the length in bytes which may be not what you need (length in bytes != length in "characters" in general, whatever definition of "character" you use, because strings are in UTF-8 in Rust, and UTF-8 is a variable width encoding).

然而,在字符串上,len()返回的字节长度可能不是您所需要的(一般来说,“字符”中的长度!=长度,无论您使用的“字符”的定义是什么,因为字符串是UTF-8,而UTF-8是一个可变宽度编码)。

Note that I also changed testLength to test_length because snake_case is the accepted convention for Rust programs.

注意,我还将testLength更改为test_length,因为snake_case是锈程序的公认约定。

#1


19  

Of course, you can do it - it's just len is not a field, it's a method:

当然,你可以这么做——只是len不是一个字段,它是一个方法:

fn test_length(arr: &[String]){
    if arr.len() >= 10 {
        // Do stuff
    }
}

If you only started learning Rust, you should read through the official book - you will also find why &[str] does not make sense (in short, str is unsized type, and you can't make an array of it; instead &str should be used for borrowed strings and String for owned strings; most likely you have a Vec<String> somewhere, and you can easily get &[String] out of it).

如果你只是开始学习铁锈,你应该通读这本书——你也会发现为什么&[str]没有意义(简而言之,str是未大小的类型,你不能对它进行数组;相反,&str应该用于借来的字符串,而用于拥有的字符串;很可能您在某个地方有一个Vec ,您可以很容易地从中获得&[String])。

I would also add that it is not clear if you want to pass a string or an array of strings into the function. If it is a string, then you should write

我还要补充一点,如果您想要将字符串或字符串数组传递到函数中,还不清楚。如果它是一个字符串,那么你应该写

fn test_length(arr: &str) {
    if arr.len() >= 10 {
        // Do stuff
    }
}

len() on a string, however, returns the length in bytes which may be not what you need (length in bytes != length in "characters" in general, whatever definition of "character" you use, because strings are in UTF-8 in Rust, and UTF-8 is a variable width encoding).

然而,在字符串上,len()返回的字节长度可能不是您所需要的(一般来说,“字符”中的长度!=长度,无论您使用的“字符”的定义是什么,因为字符串是UTF-8,而UTF-8是一个可变宽度编码)。

Note that I also changed testLength to test_length because snake_case is the accepted convention for Rust programs.

注意,我还将testLength更改为test_length,因为snake_case是锈程序的公认约定。