例子:
function a(...args: ???type of b() params???) {
b(...args)
}
Run Code Online (Sandbox Code Playgroud)
我想args成为 的b参数类型。
如果你想知道为什么我想要这个,那么它是为了代码可读性/封装。b是ed 函数,我不关心它在函数声明import层面的实现a
我有一个包含符号作为键的对象.在这种情况下如何进行解构分配?
let symbol = Symbol()
let obj = {[symbol]: ''}
let { /* how do I create a variable here, that holds the value of [symbol] property? */ } = obj
Run Code Online (Sandbox Code Playgroud)
我需要知道这是否可能,我确实知道明显而简单的解决方法,但这不是我所要求的.
我有这个简单的lambda:
std::variant<int, char> myLambda = []() { // no suitable user-defined conversion from "type" to "std::variant<int, char>" exists
std::variant<int, char> res;
if (true)
{
res = 1;
}
else
{
res = 'c';
}
return res;
};
Run Code Online (Sandbox Code Playgroud)
但是它不编译,产生错误no suitable user-defined conversion from "type" to "std::variant<int, char>" exists。我究竟做错了什么?
我偶然发现了这样的代码:
std::vector<std::unique_ptr<Fruit>> fruits;
fruits.emplace_back(new Fruit);
Run Code Online (Sandbox Code Playgroud)
(从这里)
因此,在代码中,我们将一个原始指针(new产生一个原始指针,对吗?)推入唯一指针的向量中。和代码的作品!但为什么?
因为这不能编译:
std::unique_ptr<Fruit> f = new Fruit();
Run Code Online (Sandbox Code Playgroud)
为了方便起见,这是不是有点不可思议?
此外,这种方法而不是显式的可能存在的陷阱是fruits.emplace_back(std::make_unique<Fruit>())什么?我读过这make_unique是创建唯一指针的首选方法。
到目前为止,我知道我可以成为所有struct成员const。但是我可以在某个地方写const一次并且所有成员都转向const吗?
换句话说,const即使我忘记const在变量声明中添加修饰符,我也希望所有的struct实例都是这样。
const struct Foo {}
Run Code Online (Sandbox Code Playgroud)
由于const can only be specified for objects and functions错误,以上操作无效;
这是我的最小可复制代码(操场):
struct MyStruct {
my_string: String,
}
fn accepts_string(my_string: String) {
println!("my_string: {}", my_string)
}
fn accepts_struct_reference(my_struct: &MyStruct) {
accepts_string(my_struct.my_string);
}
fn main() {
let my_struct = MyStruct {
my_string: String::from("hi"),
};
accepts_struct_reference(&my_struct);
}
Run Code Online (Sandbox Code Playgroud)
产生:
struct MyStruct {
my_string: String,
}
fn accepts_string(my_string: String) {
println!("my_string: {}", my_string)
}
fn accepts_struct_reference(my_struct: &MyStruct) {
accepts_string(my_struct.my_string);
}
fn main() {
let my_struct = MyStruct {
my_string: String::from("hi"),
};
accepts_struct_reference(&my_struct);
}
Run Code Online (Sandbox Code Playgroud)
我相信我理解为什么会发生此错误:accepts_string尝试将字符串从结构中移开。
为什么将该参考称为shared参考?与谁共享?这个形容词是否意味着存在非共享的引用?如果是,它们是什么样的?
我正在用Rust重写C ++程序,有一件事让我激动不已。在第一次迭代中,它给了我50个错误,然后我一个个地解决了它们,而当我解决了最后一个错误时,编译器给了我60个新的错误,然后我解决了它们并得到了另外几十个错误。
最后(到目前为止)的错误集似乎完全由借阅检查器生成。那么为什么会这样呢?编译过程是否存在某些层次或阶段,如果有,它们是什么?
我想知道这一点,因为我喜欢可预测性并且不喜欢情绪过山车(我也想知道这次冒险何时结束)。
我有一个非常大的结构:
struct VeryLargeStructure {
// many tens of fields here
}
Run Code Online (Sandbox Code Playgroud)
另一个结构体将其包含在字段中:
struct A {
v: VeryLargeStructure
}
Run Code Online (Sandbox Code Playgroud)
我如何从v现场转移价值......
let a = A{/* ... */}
let b = a.v;
Run Code Online (Sandbox Code Playgroud)
...无需为 构造一个新的 VeryLargeStructure 实例,因为a.v这会导致性能不佳且无用?
我知道mem::replace和mem::swap,但它们不满足上述要求。
我更喜欢安全的方法,但我怀疑没有一种方法,所以我也准备采取一种unsafe方法。
我正在解析一些文本。我需要支持unicode文本,这就是为什么我要使用String::chars迭代器:
use std::time::Instant;
fn main() {
let text = "a".repeat(10000);
let mut timer1 = 0;
let mut timer2 = 0;
let start1 = Instant::now();
for pos in 1..10000 {
let start2 = Instant::now();
let ch = text.chars().nth(pos).unwrap();
timer2 += start2.elapsed().as_millis();
}
timer1 += start1.elapsed().as_millis();
println!("timer1: {} timer2: {}", timer1, timer2);
}
Run Code Online (Sandbox Code Playgroud)
输出示例:
timer1: 4276 timer2: 133
Run Code Online (Sandbox Code Playgroud)
当我认为它们应该彼此非常接近时,为什么timer2比令人难以置信的要小timer1?
PS我已经知道那.nth很慢,不应该使用。
我是C ++的新手,我怀疑,这个问题当然不仅与有关tuple。
因此,我看了一个大致包含以下代码的教程:
#include <tuple>
std::tuple<...> t(...)
Run Code Online (Sandbox Code Playgroud)
为什么#include <tuple>呢 特别是考虑到我们明确编写的事实std::tuple。没有那#include行代码就可以很好地编译...
c++ ×4
rust ×4
const ×1
ecmascript-6 ×1
header ×1
javascript ×1
lambda ×1
performance ×1
pointers ×1
reference ×1
struct ×1
terminology ×1
types ×1
typescript ×1
variant ×1
vector ×1