【发布时间】:2018-11-23 10:42:24
【问题描述】:
我是 Rust 的初学者,刚刚学习所有权概念。
我正在使用这个函数来反转一个字符串
fn reverse(input: &str) -> String {
//we are receiving a borrowed value,
input
//get an iterator of chars from the string slice
.chars()
//Goes between two iterators.
//From the doc: double-ended iterator with the direction inverted.
.rev()
//collect into a String
.collect::<String>()
}
fn process_reverse_case(input: &str, expected: &str) {
assert_eq!(&reverse(input), expected)
}
fn main() {
process_reverse_case("robot", "tobor");
}
我想了解robot 和tobor 的所有者是谁。
- 我传递了一个借来的值。这是一个字符串切片。
- 我知道这不能被修改。因此,当我们收集反向字符串时,我认为我们正在将其收集到
argument 1或assert_eq!中。我说的对吗? - 但是随着逆向+收集的过程发生在
reverse内部,需要的内存不断增加。argument 1的assert_eq!是否说明了这一点? - 上述问题可能已由编译器在编译时解决。我对吗?
【问题讨论】:
标签: rust