我正在为我的应用程序使用消息传递架构,通常传递的类型是io::Result<Box<str>>
.有时候,我想传递一个空字符串.执行此操作时是否会发生任何分配?
fn pass_empty_string(tx: mpsc::Sender<io::Result<Box<str>>>) {
tx.send(Ok("".into()))
}
我正在为我的应用程序使用消息传递架构,通常传递的类型是io::Result<Box<str>>
.有时候,我想传递一个空字符串.执行此操作时是否会发生任何分配?
fn pass_empty_string(tx: mpsc::Sender<io::Result<Box<str>>>) {
tx.send(Ok("".into()))
}
不.控制流经过几个函数,但最终达到RawVec::allocate_in()
,有以下判断:
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
if T::IS_ZST || capacity == 0 {
Self::new_in(alloc)
}
如果字符串是常数,则为LLVM will also optimize the code away.
但请注意,这不是guaranteed.