在D中,我正在写一个文件:
File opfile = File(opdir~opname, "w");
... //first gap
opfile.writeln("somestuff");
... //second gap
opfile.writeln("otherstuff");
Run Code Online (Sandbox Code Playgroud)
在这种情况下,第一个间隙中的东西需要几分钟才能运行,第二个间隙中的东西需要几个小时,我希望在程序结束之前将"somestuff"写入文件作为理智校验.
在我看来,D使用缓冲输出,因此,所有输出在第二个间隙后立即写入.在C++中,我使用ostream :: flush在第二个间隙之前手动刷新opfile.
D中的等效操作是什么?我在std.file的文档中找不到它.
通常在D中,我想写一些类似于:
int result = 0;
foreach(someclass c; parallel(someclass_array)){
result += somefunction(c);
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,我可以将其重写为:
TaskPool.reduce!("a+b")(TaskPool.map!(somefunction)(c);
Run Code Online (Sandbox Code Playgroud)
但在其他情况下,这是不可能的,例如:
int result = 0;
someotherclass d;
int otherArg = 5;
foreach(someclass c; parallel(someclass_array)){
result += d.somefunction(c, otherArg);
}
Run Code Online (Sandbox Code Playgroud)
这将无法工作,因为某些函数将作为委托传递给map(以支持otherArg),但是委托目前不会与D中的这些指针混合使用.
我真正喜欢的是一些写作方式:
int result = 0;
foreach(someclass c; parallel(someclass_array)){
int tmp = somefunction(c);
... //something to indicate that this section is atomic
result += tmp;
... //end of atomic section.
}
Run Code Online (Sandbox Code Playgroud)
我看到D有信号量,但使用它们似乎过于笨重.我也试过atomicOp!("+ ="),但似乎未定义.是否有惯用的D方式来做到这一点?
或多或少在标题中所说的内容.我这样打电话给getaddrinfo:
struct addrinfo crossplat, *actual_addr;
crossplat.ai_family = AF_UNSPEC; //IPv4 or IPv6.
crossplat.ai_socktype = SOCK_STREAM;
if(int i = getaddrinfo("localhost", "8000", &crossplat, &actual_addr)!=0){
cerr << "error code " << i << " received. Meaning: "<< gai_strerror(i) << endl;
return -1;
}
Run Code Online (Sandbox Code Playgroud)
自豪地印刷:
error code 1 received. Meaning: Unknown error
Run Code Online (Sandbox Code Playgroud)
在我的系统上.
我系统上的getaddrinfo手册页:
RETURN VALUE
getaddrinfo() returns 0 if it succeeds, or one of the following
nonzero error codes:
EAI_ADDRFAMILY....
...The gai_strerror() function translates these error codes to a
human readable string, suitable for …Run Code Online (Sandbox Code Playgroud) Rust支持两种访问向量元素的方法:
let mut v = vec![1, 2, 3];
let first_element = &v[0];
let second_element = v.get(1);
Run Code Online (Sandbox Code Playgroud)
该get()方法返回一个Option类型,这似乎是一个有用的安全功能.类似C语法的&v[0]类型似乎更短,但放弃了安全性好处,因为无效读取会导致运行时错误,而不是产生读取超出范围的指示.
当我想要使用直接访问方法时,我不清楚,因为它似乎唯一的优点是键入更快(我保存3个字符).我还没有看到其他一些优势(也许是加速?)?我想我会保存匹配表达式的条件,但这似乎并不像成本那样提供了很多好处.
在一个新的Rust模块中,我可以写:
struct myStruct {
x : u32
}
impl myStruct {
fn new() -> myStruct{
myStruct{ x : other()}
}
fn other() -> u32 {
6
}
}
Run Code Online (Sandbox Code Playgroud)
来自其他OO语言,我希望other()能够参与其中new().也就是说,我希望能够从同一个类的另一个静态方法调用一个类的静态方法.但是,rustc会产生以下信息:
error[E0425]: cannot find function `other` in this scope
--> dummy_module.rs:9:23
|
9 | myStruct{ x : other()}
| ^^^^^ not found in this scope
Run Code Online (Sandbox Code Playgroud)
相比之下,以下Java代码编译良好:
public class myStruct{
int x;
public myStruct(){
x = other();
}
private int other(){
return 5;
}
}
Run Code Online (Sandbox Code Playgroud)
我不记得在我正在使用的Rust书中看到任何提及这一点,我似乎无法在网上找到明确的答案.我可以通过显式确定对其他人的调用来修复它myStruct::other(),但这看起来很麻烦.如果我尝试use …
也许是个愚蠢的问题.
假设我有以下内容:
class A{
int x;
int y;
virtual int get_thing(){return x;}
};
class B : public A {
int get_think(){return y;}
};
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,B :: get_thing返回x,因为重写代码有拼写错误.
如何在编译时确保在类B中覆盖了get_thing函数,以便它返回y?
在Rust编程语言的第3章中,以下代码用作Rust无法管理的一种类型推断的示例:
fn main() {
let condition = true;
let number = if condition { 5 } else { "six" };
println!("The value of number is: {}", number);
}
Run Code Online (Sandbox Code Playgroud)
解释说:
Rust需要在编译时知道
number变量的类型,确切地说,它可以在编译时验证它的类型在我们使用的任何地方都是有效的number.如果number仅在运行时确定类型,Rust将无法执行此操作; 编译器会更复杂,并且如果必须跟踪任何变量的多个假设类型,它将对代码提供更少的保证.
我不确定我理解其基本原理,因为该示例看起来像是一个简单的编译器可以推断出类型的东西.
究竟是什么让这种类型的推理变得如此困难?在这种情况下,条件的值可以在编译时明确推断(它是真的),因此数字的类型也可以(它是i32?).
如果你试图在多个编译单元中推断类型,我可以看到事情会变得多么复杂,但是这个特定的例子是否会给编译器带来很多复杂性?