我想创建一个指定次数打印"Hello"的宏.它的使用方式如下:
many_greetings!(3); // expands to three `println!("Hello");` statements
Run Code Online (Sandbox Code Playgroud)
创建该宏的天真方式是:
macro_rules! many_greetings {
($times:expr) => {{
println!("Hello");
many_greetings!($times - 1);
}};
(0) => ();
}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为编译器不计算表达式; $times - 1不计算,但作为一个新的表达式输入宏.
我试图找到有关<<和>>运算符如何处理整数的确切信息,但我找不到一个明确的答案(文档在这方面并不是那么好).
语义的两个部分对我来说并不清楚.首先,哪些位"移入"?
0b1110_1010u8 << 4 == 0b1010_0000u8)移入,或0b1110_1010u8 << 4 == 0b1010_1110u8),或此外,移位如何使用有符号整数?符号位是否也参与了班次?或者这是未指定的?
从节点文档,我正在尝试一些代码Buffer.
当我执行此语句时
const buf = Buffer.from('hello world', 'ascii');
Run Code Online (Sandbox Code Playgroud)
它引发了一个异常: TypeError: Buffer.alloc is not a function
我的节点版本是v5.7.1.
我不明白为什么?
这是错误屏幕.
我在迭代器上应用了一个闭包,我想使用stable,所以我想返回一个盒装Iterator.这样做的显而易见的方法如下:
struct Foo;
fn into_iterator(myvec: &Vec<Foo>) -> Box<dyn Iterator<Item = &Foo>> {
Box::new(myvec.iter())
}
Run Code Online (Sandbox Code Playgroud)
这是失败的,因为借用检查器无法推断出适当的生命周期.
经过一番研究,我找到了正确的方法来返回迭代器?,这让我想补充一下+ 'a:
fn into_iterator<'a>(myvec: &'a Vec<Foo>) -> Box<dyn Iterator<Item = &'a Foo> + 'a> {
Box::new(myvec.iter())
}
Run Code Online (Sandbox Code Playgroud)
但我不明白
我试着编译这段代码(Playground):
trait Family<'a> {
type Out;
}
struct U32Family;
impl<'a> Family<'a> for U32Family {
type Out = u32;
}
trait Iterator {
type Item;
fn next<'s>(&'s mut self) -> <Self::Item as Family<'s>>::Out
where
Self::Item: Family<'s>;
}
struct Foo;
impl Iterator for Foo {
type Item = U32Family;
fn next<'s>(&'s mut self) -> <Self::Item as Family<'s>>::Out
where
Self::Item: Family<'s>,
{
0u32 // <-- in real code, this is somehow calculated
}
}
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,它导致了这个错误:
error[E0308]: mismatched types
--> src/main.rs:28:9
| …Run Code Online (Sandbox Code Playgroud) 这个特征定义编译好:
trait Works {
fn foo() -> Self;
}
Run Code Online (Sandbox Code Playgroud)
但是,这会导致错误:
trait Errors {
fn foo() -> Option<Self>;
}
Run Code Online (Sandbox Code Playgroud)
error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> src/lib.rs:6:5
|
6 | fn foo() -> Option<Self>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `Self`
= note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= help: consider adding a `where Self: std::marker::Sized` bound
= note: required …Run Code Online (Sandbox Code Playgroud) 我有这个枚举类型:
enum Animal {
Dog(i32),
Cat(u8),
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个将此类型作为参数的函数.我知道(出于某种原因)输入总是一个Cat.我想实现这个目标:
fn count_legs_of_cat(animal: Animal) -> u8 {
if let Animal::Cat(c) = animal { c } else { unreachable!() }
}
Run Code Online (Sandbox Code Playgroud)
我可以写这个更短和/或更惯用的吗?
我写了一个函数来提示输入并返回结果.在此版本中,返回的字符串包含用户的尾随换行符.我想将该换行符(以及该换行符)删除后返回输入:
fn read_with_prompt(prompt: &str) -> io::Result<String> {
let stdout = io::stdout();
let reader = io::stdin();
let mut input = String::new();
print!("{}", prompt);
stdout.lock().flush().unwrap();
reader.read_line(&mut input)?;
// TODO: Remove trailing newline if present
Ok(input)
}
Run Code Online (Sandbox Code Playgroud)
仅删除单个尾随换行符的原因是此函数还将用于提示输入密码(适当使用termios以停止回显),如果某人的密码具有尾随空格,则应保留该空格.
在关于如何在字符串末尾实际删除单个换行符之后,我最终使用了trim_right_matches.然而,返回一个&str.我试图用它Cow来处理这个但是错误仍然说input变量不够长.
fn read_with_prompt<'a>(prompt: &str) -> io::Result<Cow<'a, str>> {
let stdout = io::stdout();
let reader = io::stdin();
let mut input = String::new();
print!("{}", prompt);
stdout.lock().flush().unwrap();
reader.read_line(&mut input)?;
let mut trimmed = false;
Ok(Cow::Borrowed(input.trim_right_matches(|c| {
if !trimmed …Run Code Online (Sandbox Code Playgroud) Rust crates使用语义版本控制.因此,每个具有突破性更改的版本都会导致主要版本的崩溃.一个重大更改通常被认为的东西,可能会破坏下游板条箱(代码依赖于问题的库).
然而,在Rust中,很多都有可能打破下游板条箱.例如,更改(包括仅添加)公共符号集可能是一个重大变化,因为下游包可以使用glob-imports(use foo::*;)将我们库的符号拉入其命名空间.因此,添加符号也可以打破依赖的条件箱; 看这个例子.
同样,更改(添加或更改版本)我们的依赖项集可能会破坏下游构建.您还可以想象下游包依赖于我们的一种公共类型的特定大小.如果有的话,这很少有用; 我只是想表明:如果只有下游的箱子足够努力,一切都可能是一个突破性的变化.
这有什么指导方针吗?究竟什么被认为是一个突破性的变化,什么不是(因为它被认为是"用户的错")?
rust ×9
rust-cargo ×2
bit-shift ×1
enums ×1
lifetime ×1
macros ×1
node.js ×1
rust-macros ×1
string ×1
traits ×1