6 个稳定版本
1.2.1 | 2021年12月12日 |
---|---|
1.2.0 | 2021年12月9日 |
0.2.3 |
|
0.2.1 |
|
0.0.0 |
|
#32 in #text-input
481 个月下载量
用于 3 crates
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