我们正试图将一个简单的控制台输入API推送到标准库中,以使编写简单的命令行输入变得更容易,我们需要社区决定实现的高级程度。因为这是一个相当有争议的话题(双方的数量非常均匀),所以这次投票就是为了解决这个问题。
let age: i32 = std::inputln!()?.parse().expect("Invalid age!");
let person = scan!("{} {}", String, u8).expect("Invalid input!");
let name: String;
let age: u8;
scan!("{} {}", name, age).expect("Invalid input!");
crate来进行测试:pub trait Calculator {
fn new() -> Self;
fn add(&self, a: u32, b: u32) -> u32;
}
pub struct Foo {}
impl Calculator for Foo {
fn new() -> Self {
Self {}
}
fn add(&self, a: u32, b: u32) -> u32 {
a + b
}
}
pub struct Bar {}
impl Calculator for Bar {
fn new() -> Self {
Self {}
}
fn add(&self, a: u32, b: u32) -> u32 {
if b == 0 {
a
} else {
self.add(a, b - 1) + 1
}
}
}
#[cfg(test)]
mod tests {
use super::{Bar, Calculator, Foo};
use test_case::test_case;
#[test_case(Foo::new())]
#[test_case(Bar::new())]
fn trait_tester(c: impl Calculator) {
assert_eq!(c.add(2, 3), 5);
assert_eq!(c.add(10, 43), 53);
}
}
PyASCIIObject:纯ascii字符串;PyCompactUnicodeObject:Unicode字符串;PyUnicodeObject:兼容历史API的Unicode字符串。&str:字符串切片;String:动态扩容的字符串;From 日报小组 odd-cat
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。