小编Tim*_*ann的帖子

为什么Box <[T]>在内存中需要16个字节,但是引用的切片仅需要8个字节?(在x64机器上)

考虑:

fn main() {
    // Prints 8, 8, 16
    println!(
        "{}, {}, {}",
        std::mem::size_of::<Box<i8>>(),
        std::mem::size_of::<Box<&[i8]>>(),
        std::mem::size_of::<Box<[i8]>>(),
    );
}
Run Code Online (Sandbox Code Playgroud)

为什么拥有的切片占用16个字节,而引用的切片仅占用8个字节?

rust

14
推荐指数
2
解决办法
479
查看次数

动态导入的返回类型是什么?

我有一个必须异步加载的文件,所以我创建了一个函数,它加载这个文件并返回Promise

export function load() {
    // ...
    return import(filename);
}
Run Code Online (Sandbox Code Playgroud)

这个函数的返回类型是什么?Promise<any>有效,但感觉很奇怪。我想把签名写成这样。

export function load() -> Promise<???>;
Run Code Online (Sandbox Code Playgroud)

typescript typescript2.4

13
推荐指数
2
解决办法
1万
查看次数

逻辑运算符在C中的优先级

#include <stdio.h>

int main(void) {
    int a = 0, b = 0, c = 0;
    ++a || ++b && ++c;
    printf("%d %d %d", a, b, c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是1,0,0与gcc 8.1.0.该&&的优先级应高于||.

为什么是bc还在0

c

11
推荐指数
2
解决办法
717
查看次数

如何在Rust 2018中惯用别名包装箱?

我有一个箱子foo_sys.在Rust 2015中我使用的extern crate foo_sys as foo是方便,但在Rust 2018 extern crate中不再需要了,我不想仅将其用于别名.下降时extern crate,我明白了

错误[E0463]:无法找到包装箱foo

rust rust-cargo rust-2018

10
推荐指数
2
解决办法
1907
查看次数

为什么Rust编译器不能优化Box :: downcast的Err arm?

我有一个Box<dyn Any>,我知道基础类型,所以我想在Box::downcast()()中优化测试.

首先我试过std::hint::unreachable_unchecked():

pub unsafe fn downcast() -> Box<i32> {
    let value = any();
    if let Ok(value) = value.downcast() {
        value
    } else {
        std::hint::unreachable_unchecked()
    }
}
Run Code Online (Sandbox Code Playgroud)

pub unsafe fn downcast() -> Box<i32> {
    any().downcast().map_err(|_| std::hint::unreachable_unchecked()).unwrap()
}
Run Code Online (Sandbox Code Playgroud)

rustc -C opt-level=3两个结果在该(40行中省略):

example::downcast:
        push    rbx
        sub     rsp, 16
        call    any@PLT
        mov     rbx, rax
        mov     qword ptr [rsp], rax
        mov     qword ptr [rsp + 8], rdx
        mov     rdi, rax
        call    qword ptr …
Run Code Online (Sandbox Code Playgroud)

optimization assembly compiler-optimization rust llvm-codegen

10
推荐指数
1
解决办法
279
查看次数

如何在不使用extern crate的情况下导入Rust 2018中的所有宏,派生和程序宏?

我正在尝试使用Rust Edition 2018.在Rust 2015中你使用

#[macro_use]
extern crate log;
Run Code Online (Sandbox Code Playgroud)

用于导入宏.在Rust 2018 extern crate中可能是单一的.有没有办法,从箱子里导入所有的宏extern crate?对于简单的宏,在模块中导入它很好,但复杂的宏依赖于其他几个宏,这是不方便的.

rust rust-2018

8
推荐指数
1
解决办法
946
查看次数

如何使用仅在条目空缺时构建的昂贵密钥使用Entry API?

是否可以使用EntryAPI通过a获取值AsRef<str>,但是将其插入Into<String>

这是工作示例:

use std::collections::hash_map::{Entry, HashMap};

struct Foo;

#[derive(Default)]
struct Map {
    map: HashMap<String, Foo>,
}

impl Map {
    fn get(&self, key: impl AsRef<str>) -> &Foo {
        self.map.get(key.as_ref()).unwrap()
    }

    fn create(&mut self, key: impl Into<String>) -> &mut Foo {
        match self.map.entry(key.into()) {
            Entry::Vacant(entry) => entry.insert(Foo {}),
            _ => panic!(),
        }
    }

    fn get_or_create(&mut self, key: impl Into<String>) -> &mut Foo {
        match self.map.entry(key.into()) {
            Entry::Vacant(entry) => entry.insert(Foo {}),
            Entry::Occupied(entry) => entry.into_mut(),
        }
    }
}

fn …
Run Code Online (Sandbox Code Playgroud)

hashmap rust

8
推荐指数
2
解决办法
239
查看次数


Why does an existential type require a generic instead of an associated type?

I have an existential type defined like this:

trait Collection {
    type Element;
}
impl<T> Collection for Vec<T> {
    type Element = T;
}

type Existential<T> = impl Collection<Element = T>;
Run Code Online (Sandbox Code Playgroud)

A function, which takes a type implementing a trait with an associated type, returns this type. Why does this code work:

fn return_existential<I, T>(iter: I) -> Existential<T>
where
    I: IntoIterator<Item = T>,
    I::Item: Collection,
{
    let item = iter.into_iter().next().unwrap();
    vec![item]
}
Run Code Online (Sandbox Code Playgroud)

playground

while this does not:

fn return_existential<I>(iter: I) …
Run Code Online (Sandbox Code Playgroud)

generics existential-type rust associated-types

7
推荐指数
0
解决办法
215
查看次数

在参数位置使用`impl <T> From <T> for Option <T>`是不是惯用的?

这个特性从1.12.0开始实现:

impl<T> From<T> for Option<T> {
    fn from(val: T) -> Option<T> {
        Some(val)
    }
}
Run Code Online (Sandbox Code Playgroud)

作为一个论点,这是多么惯用?考虑这个例子:

fn do_things(parameters: &Foo, optional_argument: impl Into<Option<Duration>>) {
    let optional_argument = optional_argument.into();
    // use it...
}
Run Code Online (Sandbox Code Playgroud)

如果你看到文档,它(或多或少)清楚(如果你知道,这个特性已经实现).但是如果你看到代码,你可能会感到困惑:

do_things(params, Duration::from_millis(100));
Run Code Online (Sandbox Code Playgroud)

这样可以使用还是应该避免?

rust

6
推荐指数
1
解决办法
157
查看次数