gpt4 book ai didi

rust - 匹配运算符的可能未初始化变量的借用

转载 作者:行者123 更新时间:2023-12-03 11:40:33 27 4
gpt4 key购买 nike

我正在设计一个库,该库可以将给定的字符串从乌克兰语译为英语,因此我决定使用“匹配”运算符来定义带有多个条件检查的语句。但是我碰到了Rust常见的编译器错误,但是在我的情况下是完全不可能的(至少我想是这样)。


--> src/lib.rs:188:21
|
188 | origin_mutated[i] = 'Y';
| ^^^^^^^^^^^^^^ use of possibly-uninitialized `origin_mutated`

error: aborting due to previous error

这是库的完整代码。如果我没有发现明显的问题,请把我的 Nose 碰到一个问题(因为我怀疑这可能是编译器中的错误)
pub fn transliterate(mut origin: String) -> String {
let counter: usize = origin.chars().count();
let mut j: usize = 0;
let mut i: usize = 0;
let origin_vec: Vec<char> = origin.chars().collect();
let mut origin_mutated: Vec<char>;
if j <= counter{
while j <= counter {
match origin_vec[j] {
'А' => {
origin_mutated[i] = 'A';
i+=1;
j+=1;
},
'Б' => {
origin_mutated[i] = 'B';
j+=1;
i+=1;
},
'В' => {
origin_mutated[i] = 'V';
i+=1;
j+=1;
},
'Г' => {
origin_mutated[i] = 'H';
i+=1;
j+=1;
},
'Ґ' => {
origin_mutated[i] = 'G';
i+=1;
j+=1;
},
'Д' => {
origin_mutated[i] = 'D';
i+=1;
j+=1;
},
'Е' => {
origin_mutated[i] = 'E';
i+=1;
j+=1;
},
'Є' => {
origin_mutated[i] = 'Y';
i+=1;
j+=1;
origin_mutated[i] = 'e';
i+=1;
},
'Ж' => {
origin_mutated[i] = 'Z';
i+=1;
j+=1;
origin_mutated[i] = 'h';
i+=1;
},
'З' => {
origin_mutated[i] = 'Z';
i+=1;
j+=1;
},
'И' => {
origin_mutated[i] = 'Y';
i+=1;
j+=1;
},
'І' => {
origin_mutated[i] = 'I';
i+=1;
j+=1;
},
'Ї' => {
origin_mutated[i] = 'Y';
i+=1;
j+=1;
origin_mutated[i] = 'i';
i+=1;
},
'Й' => {
origin_mutated[i] = 'Y';
i+=1;
j+=1;
},
'К' => {
origin_mutated[i] = 'K';
i+=1;
j+=1;
},
'Л' => {
origin_mutated[i] = 'L';
i+=1;
j+=1;
},
'М' => {
origin_mutated[i] = 'M';
i+=1;
j+=1;
},
'Н' => {
origin_mutated[i] = 'N';
i+=1;
j+=1;
},
'О' => {
origin_mutated[i] = 'O';
i==1;
j+=1;
},
'П' => {
origin_mutated[i] = 'P';
i+=1;
j+=1;
},
'Р' => {
origin_mutated[i] = 'R';
i==1;
j+=1;
},
'С' => {
origin_mutated[i] = 'S';
i==1;
j+=1;
},
'Т' => {
origin_mutated[i] = 'T';
i==1;
j+=1;
},
'У' => {
origin_mutated[i] = 'U';
i+=1;
j+=1;
},
'Ф' => {
origin_mutated[i] = 'F';
i==1;
j+=1;
},
'Х' => {
origin_mutated[i] = 'K';
i+=1;
j==1;
origin_mutated[i] = 'h';
i+=1;
},
'Ц' => {
origin_mutated[i] = 'T';
i+=1;
j+=1;
origin_mutated[i] = 's';
i+=1;
},
'Ч' => {
origin_mutated[i] = 'C';
i+=1;
j+=1;
origin_mutated[i] = 'h';
i+=1;
},
'Ш' => {
origin_mutated[i] = 'S';
i+=1;
j+=1;
origin_mutated[i] = 'h';
i+=1;
},
'Щ' => {
origin_mutated[i] = 'S';
i+=1;
j==1;
origin_mutated[i] = 'h';
i+=1;
origin_mutated[i] = 'c';
i+=1;
origin_mutated[i] = 'h';
i+=1;
},
'Ю' => {
origin_mutated[i] = 'Y';
i+=1;
j+=1;
origin_mutated[i] = 'u';
i+=1;
},
'Я' => {
origin_mutated[i] = 'Y';
i+=1;
j+=1;
origin_mutated[i] = 'a';
i+=1;
},
_ => {
j+=1;
}
}
}
}
else if j > counter{
origin_mutated[i] = '\n';
}
else {
origin = origin_mutated.into_iter().collect();
}
//origin = origin_mutated.into_iter().collect();
(origin)
}

最佳答案

发生错误的原因是此行不会创建Vec:

let mut origin_mutated: Vec<char>;

它创建了一个可以容纳Vec的变量,但还没有,甚至没有零长度的变量。就像在说
let a: i32;

它没有值(value)。你可能是说
let mut origin_mutated: Vec<char> = Vec::new();

关于rust - 匹配运算符的可能未初始化变量的借用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60996303/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com