文档指出:
Run Code Online (Sandbox Code Playgroud)assert_eq!(usize::max_value(), 18446744073709551615);
但是当我运行一个简单的测试时:
use std::usize;
fn main() {
println!("{}", usize::max_value());
}
Run Code Online (Sandbox Code Playgroud)
它打印:4294967295
我的项目仅被初始化并添加了 2 行use std::usize;,println!("{}", usize::max_value());没有任何其他更改。
我的输出rustc --version --verbose:
assert_eq!(usize::max_value(), 18446744073709551615);
Run Code Online (Sandbox Code Playgroud)
删除 Rust 并使用 64 位 Windows rustup 安装程序重新安装后,我得到:
use std::usize;
fn main() {
println!("{}", usize::max_value());
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行时rustup toolchain list它会打印一个项目:
rustc 1.41.1 (f3e1a954d 2020-02-24)
binary: rustc
commit-hash: f3e1a954d2ead4e2fc197c7da7d71e6c61bad196
commit-date: 2020-02-24
host: i686-pc-windows-msvc
release: 1.41.1
LLVM version: 9.0
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
我有代码(游乐场):
use std::collections::HashMap;
// We have some arbitrary struct (given values just placeholders)
struct SomeStruct {
x: f32,
y: usize,
}
fn main() {
// We have some hashmap which contains the names (keys) and properties (values) of items.
// These are known at compile time.
let hashmap: HashMap<&str, SomeStruct> = vec![
("a", SomeStruct { x: 2., y: 2 }),
("b", SomeStruct { x: 3.5, y: 1 }),
("c", SomeStruct { x: 0., y: 5 }),
]
.into_iter() …Run Code Online (Sandbox Code Playgroud) 这段代码返回min,这怎么可能?
if(prices[i] == 1000 && min == 53){
if(prices[i] < min){
return min;
}
return prices[i];
}
Run Code Online (Sandbox Code Playgroud) 所以我需要获取一些 GLSL 的 SPIR-V 代码
我能找到的关于该主题的每个资源都涉及为图形等设置整个环境。
在最简单的情况下,当我有一些 GLSL 代码并且我想获取 SPIR-V 代码时,最好的方法是什么?
我已经做了一些搜索,但我似乎无法找到从截断正态分布中采样的合理方法。
没有截断我正在做:
samples = [np.random.normal(loc=x,scale=d) for (x,d) in zip(X,D)]
Run Code Online (Sandbox Code Playgroud)
X并D成为浮动列表。
目前我正在实施截断:
def truncnorm(loc,scale,bounds):
s = np.random.normal(loc,scale)
if s > bounds[1]:
return bounds[1]
elif s < bounds[0]:
return bounds[0]
return s
samples = [truncnorm(loc=x,scale=d,bounds=b) for (x,d,b) in zip(X,D,bounds)]
Run Code Online (Sandbox Code Playgroud)
bounds是一个元组列表(min,max)
这种方法感觉有点尴尬,所以想知道是否有更好的方法?
尝试序列化时Option<chrono::DateTime<Utc>>遇到错误:
error[E0308]: mismatched types
--> src/main.rs:39:14
|
39 | #[derive(Serialize, Debug)]
| ^^^^^^^^^ expected struct `DateTime`, found enum `std::option::Option`
|
= note: expected reference `&DateTime<Utc>`
found reference `&'__a std::option::Option<DateTime<Utc>>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
Run Code Online (Sandbox Code Playgroud)
代码(游乐场):
error[E0308]: mismatched types
--> src/main.rs:39:14
|
39 | #[derive(Serialize, Debug)]
| ^^^^^^^^^ expected struct `DateTime`, found enum `std::option::Option`
|
= note: expected reference `&DateTime<Utc>`
found reference `&'__a …Run Code Online (Sandbox Code Playgroud) 我试图使用futures::future::select_ok(游乐场):
use std::time::Duration;
use tokio; // 1.16.1
use futures; // 0.3.19
#[tokio::main]
async fn main() {
let first_future = async {
tokio::time::sleep(Duration::from_secs(1)).await;
Ok(3)
};
let second_future = async {
tokio::time::sleep(Duration::from_millis(100)).await;
Err(())
};
let third_future = async {
tokio::time::sleep(Duration::from_secs(300)).await;
Ok(3)
};
futures::future::select_ok(&[first_future,second_future.await,third_future]).await;
}
Run Code Online (Sandbox Code Playgroud)
并遇到错误:
error[E0308]: mismatched types
--> src/main.rs:19:47
|
7 | let first_future = async {
| ______________________________-
8 | | tokio::time::sleep(Duration::from_secs(1)).await;
9 | | Ok(3)
10 | | };
| |_____- the expected `async` block …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我正在使用的库中调试复杂的程序宏。
由于我不能使用带有宏的调试器,并且各种宏扩展工具在这里被证明是无用的,我正在寻找替代方案。
程序宏是否可以像经过适当调试的函数一样运行?我想象将结果存储proc_macro::TokenStream在一个变量中。
我想在编译时检查实现中使用的切片是否From具有特定大小。
(游乐场)
#[derive(Debug)]
struct Pixel {
r: u8,
g: u8,
b: u8,
}
impl From<&[u8]> for Pixel {
fn from(arr: &[u8]) -> Pixel {
Pixel {
r: arr[0],
g: arr[1],
b: arr[2],
}
}
}
fn main() {
println!("Hello, world!");
let arr: [u8; 9] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let pixels: Vec<Pixel> = arr.chunks_exact(3).map(Pixel::from).collect();
println!("{:.?}", pixels);
}
Run Code Online (Sandbox Code Playgroud)
这并不像我想要的那么具体。我想尽可能清楚地检查arr传递给is 3 个元素(在编译时)。Pixel::from<&[u8]>()
想到了assert!(arr.len()==3),但这在运行时检查。
所以我想也许我可以通过( Playground )进行转换:
impl From<[u8; …Run Code Online (Sandbox Code Playgroud) 对于大多数错误,我经常会知道我需要朝哪个方向前进,但在这里我不知道.
那么让我们从断点本身开始:
使用线程库的任何交互都是相对较少的,这里是所有这些:包含:
#include <thread>
Run Code Online (Sandbox Code Playgroud)
用法:
void evolve(double* bestN, double* bestP, double* bestM, double* bestQ, double* bestWinConstant) {
std::thread threadArray[threadAmount];
for (int i = 0; i < 100000; i++) {
for (int t = 0; t < threadAmount; t++) {
if (gameArrayInUse[t] == 0) {
copyArray(gameArray[t], startArray);
gameArrayInUse[t] = 1;
threadArray[t] = std::thread(playGames, i, t);
std::cout << "------------New Thread Spawned, No: " << t << std::endl;
break;
}
if (t == threadAmount - 1) {
t = -1;
}
} …Run Code Online (Sandbox Code Playgroud) 看来我在理解上遇到了相当大的缺乏。我知道最好的做法是在类中使用私有变量并通过所述类外部的公共 getter 访问它。现在,当使用 C# 的默认 getter 方法 ( private string Image { get; }) 时,我无法访问Image此类 ( Console.WriteLine(items[i].Image);) 之外的变量。
虽然我可以编写一个自定义的公共 getter,但这似乎很荒谬,因为在私有变量上有一个除了返回变量之外什么也不做的私有 getter 似乎完全多余,因此让我觉得我错过了一些东西。
由于您不能简单地执行以下操作:
if option.is_some() && option == 1 {...}
Run Code Online (Sandbox Code Playgroud)
因为如果option.is_some() == false第二次比较会出错。
一般认为做这种事情的最佳方法是什么?
(我现在做正确的:if option.is_some() { if option == 1 {...} })
我从一个Vec已排序的节点开始,然后使用此排序将这些节点在二叉树中链接在一起,然后返回基本结构
// Test name
#[derive(Clone)]
struct Struct {
parent: Option<Rc<RefCell<Struct>>>,
superscript: Option<Rc<RefCell<Struct>>>,
subscript: Option<Rc<RefCell<Struct>>>,
height: u32,
center: u32,
symbols: VecDeque<u8>
}
Run Code Online (Sandbox Code Playgroud)
最终得到由上述 s 形成的二叉树Struct。此时,这些Structs 是唯一拥有的,所以我认为我可以从 using 转换Rc<RefCell<Struct>>为RefCell<Struct>(认为Box<Struct>由于内部可变性而不起作用?),但我不确定这如何或是否有助于解决我遇到的问题。
之后,我需要以一种新颖的方式迭代 s Struct,并通过调用 来改变整个递归过程中symbols属于各个s 的各种内容。Struct.pop_front()
我当前的实现这样做会导致thread 'main' panicked at 'already borrowed: BorrowMutError'.
它的功能(请原谅复杂的逻辑):
fn traverse_scripts(row: Rc<RefCell<Struct>>) {
if let Some(superscript_row) = &row.borrow().superscript {
if let Some(superscript_symbol) = superscript_row.borrow().symbols.front() {
if let …Run Code Online (Sandbox Code Playgroud)