这个函数的实现是什么:
fn unbox<T>(value: Box<T>) -> T {
// ???
}
Run Code Online (Sandbox Code Playgroud)
文档中唯一看起来像我想要的功能Box::into_raw.以下将进行类型检查:
fn unbox<T>(value: Box<T>) -> T {
*value.into_raw()
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误error[E0133]: dereference of raw pointer requires unsafe function or block.将其包装在一个unsafe { ... }块中可以修复它.
fn unbox<T>(value: Box<T>) -> T {
unsafe { *value.into_raw() }
}
Run Code Online (Sandbox Code Playgroud)
这是正确的实施吗?如果是这样,为什么它不安全?这是什么意思?
也许这个问题显示了我对如何Box实际工作的一般不确定性.
如何获取从另一个函数struct返回的 a的值Result?下面举例。
#[derive(Debug)]
pub struct Keypair(ed25519_dalek::Keypair);
pub fn keypair_from_seed(seed: &[u8]) -> Result<Keypair, Box<dyn error::Error>> {
let dalek_keypair = ed25519_dalek::Keypair { secret, public };
Ok(Keypair(dalek_keypair))
}
Run Code Online (Sandbox Code Playgroud)
fn main(){
//here seed_bytes is mnemonics
let sk = keypair_from_seed(&seed_bytes);
//sk contains the secret key and public key, i want to extract it different secret key & public key
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含两个变体的枚举:
enum DatabaseType {
Memory,
RocksDB,
}
Run Code Online (Sandbox Code Playgroud)
如果在检查参数是否为DatabaseType::Memory或的函数内部进行条件化,我需要什么DatabaseType::RocksDB?
fn initialize(datastore: DatabaseType) -> Result<V, E> {
if /* Memory */ {
//..........
} else if /* RocksDB */ {
//..........
}
}
Run Code Online (Sandbox Code Playgroud)