#text-input #input #format #stdin #parser #sscanf

scanf

解析文本输入(与 print! 和 format! 互为逆操作)

6 个稳定版本

1.2.1 2021年12月12日
1.2.0 2021年12月9日
0.2.3 2021年12月1日
0.2.1 2021年11月30日
0.0.0 2021年11月27日

#32 in #text-input

Download history 181/week @ 2024-03-03 217/week @ 2024-03-10 197/week @ 2024-03-17 128/week @ 2024-03-24 133/week @ 2024-03-31 114/week @ 2024-04-07 125/week @ 2024-04-14 139/week @ 2024-04-21 142/week @ 2024-04-28 183/week @ 2024-05-05 116/week @ 2024-05-12 112/week @ 2024-05-19 125/week @ 2024-05-26 122/week @ 2024-06-02 101/week @ 2024-06-09 119/week @ 2024-06-16

481 个月下载量
用于 3 crates

Unlicense

18KB
388

Scanf

如果你知道 C 中的它,功能相同但具有内存安全性。

let mut number: u32 = 0;
let mut name: String = String::new();
if scanf!("{},{}", number, name).is_ok() {
    println!("Input is: {} and {}", number, name);
}
let input = "5,something";
let mut number: u32 = 0;
let mut name: String = String::new();
if let Err(error) = sscanf!(input, "{},{}", number, name) {
    panic!("Error {} using sscanf!", error);
}

请参阅文档中的更多示例


lib.rs:

scanf! & sscanf!

与 C 中的类似,但具有内存安全性。

示例

use scanf::scanf;

let mut product: String = String::new();
let mut price: f32 = 0.0;
println!("Insert product and price (product: price):");
if scanf!("{}: {}", product, price).is_ok() {
    println!("Price of {} is {:.2}", product, price);
}
use scanf::sscanf;

let input: &str = "Candy: 2.75";
let mut product: String = String::new();
let mut price: f32 = 0.0;
println!("Insert product and price (product: price):");
sscanf!(input, "{}: {}", product, price);
println!("Price of {} is {:.2}", product, price);

可以在格式字符串中指定类型

let mut product: String = String::new();
let mut price: f32 = 0.0;
println!("Insert product and price (product: price):");
scanf!("{string}: {f32}", product, price);

还可以转义括号

let input: &str = "{Candy}";
let mut product: String = String::new();
sscanf!(input, "{{{}}}", product);
assert_eq!(product, "Candy");

示例已编译,sscanf 的示例也作为测试运行。如果您在使用示例代码时遇到问题,请创建一个问题

依赖

~1MB
~20K SLoC