相关疑难解决方法(0)

什么"`str`没有在编译时知道的常量大小"是什么意思,什么是最简单的修复方法?

我正在尝试操作从函数参数派生的字符串,然后返回该操作的结果:

fn main() {
    let a: [u8; 3] = [0, 1, 2]; 
    for i in a.iter() {
        println!("{}", choose("abc", *i));
    }
}

fn choose(s: &str, pad: u8) -> String {
    let c = match pad {
        0 => ["000000000000000", s].join("")[s.len()..],
        1 => [s, "000000000000000"].join("")[..16],
        _ => ["00", s, "0000000000000"].join("")[..16],
    };
    c.to_string()
}
Run Code Online (Sandbox Code Playgroud)

在构建时,我收到此错误:

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
 --> src\main.rs:9:9
  |
9 |     let c = match pad {
  |         ^ `str` does not have a …
Run Code Online (Sandbox Code Playgroud)

string pointers rust

7
推荐指数
1
解决办法
4242
查看次数

标签 统计

pointers ×1

rust ×1

string ×1