我已经阅读了几个关于 SO 的答案,并收集了这些用例:
panic!函数但我仍然不清楚为什么我们需要这样定义函数:
fn func() -> ! {
panic!("Error!");
}
Run Code Online (Sandbox Code Playgroud)
如果它的工作方式与此相同(没有感叹号):
fn func() {
panic!("Error!");
}
Run Code Online (Sandbox Code Playgroud)
同时,为什么我们需要!在具有无限循环的函数中使用?看起来这个签名并没有带来任何真实的使用信息。
我在玩布尔值,最后得到了这行代码:
std::cout << true && false;
由于某种原因,它产生1. 这怎么可能,如果&&要求双方都为真,才能产生1?
c++ boolean-logic boolean operator-precedence logical-operators
我有这段简单的代码:
let val: u8 = 255 + 1;
println!("{}", val);
Run Code Online (Sandbox Code Playgroud)
这里据说这样的代码如果使用--releaseflag运行的话会正常编译。
我通过 运行此代码cargo run --release,并且仍然看到检查:
error: this arithmetic operation will overflow
--> src/main.rs:2:19
|
2 | let val: u8 = 255 + 1;
| ^^^^^^^ attempt to compute `u8::MAX + 1_u8`, which would overflow
|
= note: `#[deny(arithmetic_overflow)]` on by default
error: could not compile `rust-bin` due to previous error
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我试图了解 rust nightly 和 beta 版本之间的区别。它们似乎都适合需要使用实验性功能的情况,但我无法真正找到确切的区别。
所以我正在探索 Rust,并且我已经了解了常量和不可变变量之间的技术差异。但似乎不可变变量可以做常量可以做的所有事情。那么,如果不可变变量可以完全替代常量,那么常量存在的意义何在呢?
我有以下代码:
#include <stdio.h>
int main(void) {
int array[0];
printf("%d", array);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们知道,数组总是指向它的第一项,但在这个例子中我们没有项,但是这段代码会产生一些内存地址。它指向什么?
我一直在测试全局变量,定义和声明,我在这种情况下停了下来:
主文件:
#include "stdio.h"
void func(void);
int a;
int main(void) {
a = 20;
printf("in main: %d\n", a);
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
添加.c:
#include <stdio.h>
void func(void);
int a;
void func() {
printf("in add: %d\n", a);
}
Run Code Online (Sandbox Code Playgroud)
所以在 C 中
int a;
Run Code Online (Sandbox Code Playgroud)
既表示声明又表示定义,但我们知道不允许多次定义变量。那么,如果我们有两个定义和两个声明,为什么这段代码会编译a呢?我在 CLion 中工作,当我在 main 中按“转到定义/声明”时a,它会将指针移至aadd.c,当我在 add.c 中执行相同操作时,它会移回 main.c,因此我无法理解这里发生了什么。
我正在尝试创建一个伪随机生成器 API,但是 xorshift 生成的数字具有非随机性。您可以在此处查看算法和测试:
#include <stdio.h>
#include <stdlib.h>
/* basic random stream structure */
typedef struct Random64 {
uint64_t seed;
uint64_t current;
} Random64;
/* returns a new stream of uint64_t values */
Random64 new_rand64(uint64_t seed) {
Random64 new_stream;
if (seed == 0) {
perror("SEED MUST BE A NON-ZERO VALUE");
}
new_stream.seed = new_stream.current = seed;
return new_stream;
}
/* returns the next random value from sequence initialized with seed */
uint64_t next64(Random64* stream) {
uint64_t cur = stream->current;
cur …Run Code Online (Sandbox Code Playgroud) rust ×5
c ×3
algorithm ×1
arrays ×1
boolean ×1
c++ ×1
constants ×1
declaration ×1
definition ×1
deprecated ×1
function ×1
immutability ×1
integer ×1
memory ×1
panic ×1
pointers ×1
random ×1
rust-cargo ×1
rustup ×1
size ×1
variables ×1
xor ×1