考虑:
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个字节?
我有一个必须异步加载的文件,所以我创建了一个函数,它加载这个文件并返回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) #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.该&&的优先级应高于||.
为什么是b和c还在0?
我有一个箱子foo_sys.在Rust 2015中我使用的extern crate foo_sys as foo是方便,但在Rust 2018 extern crate中不再需要了,我不想仅将其用于别名.下降时extern crate,我明白了
错误[E0463]:无法找到包装箱
foo
我有一个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
我正在尝试使用Rust Edition 2018.在Rust 2015中你使用
#[macro_use]
extern crate log;
Run Code Online (Sandbox Code Playgroud)
用于导入宏.在Rust 2018 extern crate中可能是单一的.有没有办法,从箱子里导入所有的宏extern crate?对于简单的宏,在模块中导入它很好,但复杂的宏依赖于其他几个宏,这是不方便的.
是否可以使用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) str::to_ascii_lowercase返回一个字符串。为什么它不返回Cow<str>类似to_string_lossyor 的值String::from_utf8_lossy?
这同样适用于str::to_ascii_uppercase.
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)
while this does not:
fn return_existential<I>(iter: I) …Run Code Online (Sandbox Code Playgroud) 这个特性从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 ×8
rust-2018 ×2
api-design ×1
assembly ×1
c ×1
generics ×1
hashmap ×1
llvm-codegen ×1
optimization ×1
rust-cargo ×1
string ×1
typescript ×1