根据我对生命周期的理解,如果函数的调用者在参数上指定生命周期,我可以返回具有该生命周期的类型。
即使有省略,这也是有效的:
pub fn substr(s: &str) -> &str {
&s[0..1]
}
pub fn substr_ex<'a>(s: &'a str) -> &'a str {
&s[0..1]
}
Run Code Online (Sandbox Code Playgroud)
但这并没有:
use std::fmt::Arguments;
pub fn as_format_arg<'a, T: 'a + ?Sized + Debug>(t: &'a T) -> Arguments<'a> {
format_args!("{:?}", t)
}
Run Code Online (Sandbox Code Playgroud)
pub fn substr(s: &str) -> &str {
&s[0..1]
}
pub fn substr_ex<'a>(s: &'a str) -> &'a str {
&s[0..1]
}
Run Code Online (Sandbox Code Playgroud)
这是一个错误吗?还是我对生命的理解有误?
婴儿围栏:https://play.rust-lang.org/ ?gist=5a7cb4c917b38e012f20c771893f8b3b&version=nightly
我正在研究一个程序宏,我发现当程序宏发生恐慌时,编译器不会提供有关 proc-macro crates 的信息。我试图覆盖panic!打印一个位置:
macro_rules! std_panic {
($($args:tt)+) => {{
panic!($($args)*);
}};
}
/// panic! with location reporting.
macro_rules! panic {
($($args:tt)+) => {{
std_panic!("{}\n --> {}:{}:{}", format_args!($($args)*), file!(), line!(), column!());
}};
}
Run Code Online (Sandbox Code Playgroud)
但是编译器失败了
macro_rules! std_panic {
($($args:tt)+) => {{
panic!($($args)*);
}};
}
/// panic! with location reporting.
macro_rules! panic {
($($args:tt)+) => {{
std_panic!("{}\n --> {}:{}:{}", format_args!($($args)*), file!(), line!(), column!());
}};
}
Run Code Online (Sandbox Code Playgroud)
我将限制设置65536为证明这与递归扩展有关。
根据The Rust Programming Language , first edition的宏章节,我自己panic!的对 …
我需要编写类似于compiletest的东西.Compiletest通过调用rustc进程并捕获stdout/stderr来工作.我的项目还没有运行二进制文件,所以我需要另一种方法来创建子进程.
我不能使用频道或stdout,因为它不受我的代码控制.