我应该使用 last_mut() 和 Option 在我的 Rust 代码中更改什么?
What I should change in my Rust code with last_mut() and Option?
我的 Rust 代码解决了将给定字符串拆分成一对椅子的问题。起初我使用了 last()
但在更改最后一个元素时出现错误。我将 last()
替换为 last_mut()
.
fn solution(s: &str) -> Vec<String> {
let mut result: Vec<String> = vec!["".to_string()];
for member in s.chars() {
if result.last_mut().len() < 2 {
result.last_mut().push_str(member.to_string());
} else {
result.push("".to_string());
result.last_mut().push_str(member.to_string());
}
}
if result.last_mut().len() < 2 {
result.last_mut().push_str("_");
}
result
}
此代码必须像示例中那样 return 成对的椅子
solution("abcdef") // should return ["ab", "cd", "ef"]
solution("abcdefg") // should return ["ab", "cd", "ef", "g_"]
但它 return 是错误
error[E0599]: no method named `len` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:6:30
|
6 | if result.last_mut().len() < 2 {
| ^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `push_str` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:7:31
|
7 | result.last_mut().push_str(member.to_string());
| ^^^^^^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `push_str` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:10:31
|
10 | result.last_mut().push_str(member.to_string());
| ^^^^^^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `len` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:13:26
|
13 | if result.last_mut().len() < 2 {
| ^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `push_str` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:14:27
|
14 | result.last_mut().push_str("_");
| ^^^^^^^^ method not found in `Option<&mut String>`
我应该在我的代码中替换什么以使其与 Option
一起工作?这对我来说是新建筑。谢谢!
last_mut
(和 last
)return 和 Option
因为就编译器而言 向量总是空的 .
在你的情况下,这不可能通过构造(因为你创建了一个非空的 vec 然后总是附加到它),但编译器不知道所以你可以:
unwrap
获取它包含的引用的选项
- 或使用常规索引,例如
&mut result[result.len()-1]
或者,一种更简单的构造是从 Vec
构建您的“最后一项”,然后在完成后将其推入。这样编译器就可以知道它在每一点都是有效的。
let mut result = vec![];
let mut current = String::new();
for member in s.chars() {
if current.len() < 2 {
current.push(member);
} else {
result.push(current);
current = member.to_string();
}
}
if current.len() > 0 {
if current.len() == 1 {
current.push('_');
}
result.push(current)
}
另请注意:String::push
采用 char
,您无需将 char
转换为 String
即可连接它们。另外请注意,在组合角色和朋友的情况下,您的代码方式已损坏。
如果在将 String
推入 Vec
之前对 String
执行所有更改,您将会轻松很多。这是一个更简单、更清洁的解决方案:
fn solution(s: &str) -> Vec<String> {
let mut result = Vec::new();
let mut pair = String::new();
for c in s.chars() {
if pair.len() == 2 {
result.push(pair);
pair = String::new();
}
pair.push(c);
}
if pair.len() == 2 {
result.push(pair);
} else if pair.len() == 1 {
pair.push('_');
result.push(pair)
}
result
}
fn main() {
assert_eq!(solution("abcdef"), vec!["ab", "cd", "ef"]);
assert_eq!(solution("abcdefg"), vec!["ab", "cd", "ef", "g_"])
}
我的 Rust 代码解决了将给定字符串拆分成一对椅子的问题。起初我使用了 last()
但在更改最后一个元素时出现错误。我将 last()
替换为 last_mut()
.
fn solution(s: &str) -> Vec<String> {
let mut result: Vec<String> = vec!["".to_string()];
for member in s.chars() {
if result.last_mut().len() < 2 {
result.last_mut().push_str(member.to_string());
} else {
result.push("".to_string());
result.last_mut().push_str(member.to_string());
}
}
if result.last_mut().len() < 2 {
result.last_mut().push_str("_");
}
result
}
此代码必须像示例中那样 return 成对的椅子
solution("abcdef") // should return ["ab", "cd", "ef"]
solution("abcdefg") // should return ["ab", "cd", "ef", "g_"]
但它 return 是错误
error[E0599]: no method named `len` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:6:30
|
6 | if result.last_mut().len() < 2 {
| ^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `push_str` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:7:31
|
7 | result.last_mut().push_str(member.to_string());
| ^^^^^^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `push_str` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:10:31
|
10 | result.last_mut().push_str(member.to_string());
| ^^^^^^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `len` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:13:26
|
13 | if result.last_mut().len() < 2 {
| ^^^ method not found in `Option<&mut String>`
error[E0599]: no method named `push_str` found for enum `Option<&mut String>` in the current scope
--> src/lib.rs:14:27
|
14 | result.last_mut().push_str("_");
| ^^^^^^^^ method not found in `Option<&mut String>`
我应该在我的代码中替换什么以使其与 Option
一起工作?这对我来说是新建筑。谢谢!
last_mut
(和 last
)return 和 Option
因为就编译器而言 向量总是空的 .
在你的情况下,这不可能通过构造(因为你创建了一个非空的 vec 然后总是附加到它),但编译器不知道所以你可以:
unwrap
获取它包含的引用的选项- 或使用常规索引,例如
&mut result[result.len()-1]
或者,一种更简单的构造是从 Vec
构建您的“最后一项”,然后在完成后将其推入。这样编译器就可以知道它在每一点都是有效的。
let mut result = vec![];
let mut current = String::new();
for member in s.chars() {
if current.len() < 2 {
current.push(member);
} else {
result.push(current);
current = member.to_string();
}
}
if current.len() > 0 {
if current.len() == 1 {
current.push('_');
}
result.push(current)
}
另请注意:String::push
采用 char
,您无需将 char
转换为 String
即可连接它们。另外请注意,在组合角色和朋友的情况下,您的代码方式已损坏。
如果在将 String
推入 Vec
之前对 String
执行所有更改,您将会轻松很多。这是一个更简单、更清洁的解决方案:
fn solution(s: &str) -> Vec<String> {
let mut result = Vec::new();
let mut pair = String::new();
for c in s.chars() {
if pair.len() == 2 {
result.push(pair);
pair = String::new();
}
pair.push(c);
}
if pair.len() == 2 {
result.push(pair);
} else if pair.len() == 1 {
pair.push('_');
result.push(pair)
}
result
}
fn main() {
assert_eq!(solution("abcdef"), vec!["ab", "cd", "ef"]);
assert_eq!(solution("abcdefg"), vec!["ab", "cd", "ef", "g_"])
}