gpt4 book ai didi

rust - 如何在 Rust 中拆分我作为输入的字符串 read!()

转载 作者:行者123 更新时间:2023-11-29 08:17:28 25 4
gpt4 key购买 nike

我想根据其中的空格拆分作为输入提供的 String。我使用了 split_whitespaces() 函数,但是当我在自定义输入上使用此函数时,它只会给我第一个 String 切片。

let s:String = read!();
let mut i:usize = 0;
for token in s.split_whitespace() {
println!("token {} {}", i, token);
i+=1;
}

我错过了什么?

最佳答案

据我所知,read! 不是标准的宏。快速搜索显示可能来自 text_io crate (如果您使用的是外部 crate ,您应该在问题中说明)。

来自那个 crate 中的文档:

The read!() macro will always read until the next ascii whitespace character (\n, \r, \t or space).

所以您所看到的是设计使然。

如果你想从 stdin 中读取整行,你可以试试标准函数 std::Stdin::read_line .

关于rust - 如何在 Rust 中拆分我作为输入的字符串 read!(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363253/

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