我正在尝试从我的 Items 文件中导入脚本,但我一直收到错误
from .Items.Quest1_items import *
Run Code Online (Sandbox Code Playgroud)
给
from .Items.Quest1_items import *
ImportError: attempted relative import with no known parent package
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
这是我的项目树,我从 main.py 文件运行脚本
Quest1/
|
|- main.py
|
|- Items/
| |- __init__.py
| |- Quest1_items.py
Run Code Online (Sandbox Code Playgroud) 如果我以这种方式生成 10 个随机数,它会起作用(它会产生不同的值)
fn main() {
let seed = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system time cannot be before unix epoch")
.as_millis() as u64;
let mut rng = oorandom::Rand32::new(seed);
for _ in 0..10 {
println!("Your random number is: {}", &rng.rand_range(0..4));
}
Run Code Online (Sandbox Code Playgroud)
但如果我将它们分解到一个结构中,生成的值总是相同的:
use oorandom::{self, Rand32};
struct Util {
rng: Rand32,
}
impl Util {
pub fn new() -> Self {
let seed = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
println!("new color util {}", seed);
let rng = Rand32::new(seed);
Util { rng }
} …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以将 an 转换Option<Result<T, E>>为 an ,并在出现错误时Option<T>返回?Err
我生成了一个数字数组。我想删除重复项。在 javascript 中,我可以使用[...new Set(arr)]并完成工作。
在 Rust 中,到目前为止我还没有找到实现这一目标的简单方法。
我写过:
use rand::{thread_rng, Rng};
use itertools::Itertools;
fn main() {
let mut arr:Vec<u8> = Vec::new();
for _ in 0..10 {
arr.push(thread_rng().gen_range(0..10))
}
println!("random {:?}", arr);
arr.iter().unique();
println!("unique {:?}", arr);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
random [7, 0, 3, 6, 7, 7, 1, 1, 8, 6]
unique [7, 0, 3, 6, 7, 7, 1, 1, 8, 6]
Run Code Online (Sandbox Code Playgroud)
所以我试图在另一个变量中获得“无重复”结果:
random [7, 0, 3, 6, 7, 7, 1, 1, 8, 6]
unique [7, 0, 3, 6, 7, 7, …Run Code Online (Sandbox Code Playgroud) 假设我有一个像这样的代码片段:
type 'a mylist = | Nil | Cons of 'a * 'a mylist
let rec last l =
match l with
| Nil -> None
| Cons(x, Nil) -> Some(x)
| Cons(x, xs) -> last xs
Run Code Online (Sandbox Code Playgroud)
显然,这个模式匹配mylist-Nil和的构造函数Cons。我的问题是你将如何实施这样的事情。你不能只提供值,Cons直到找到一些匹配的值l,所以我假设 OCaml 编译器实现了类似“反向构造函数”的东西,它接受类型的值mylist并尝试将其转换为组成部分?
例如,在 psudo-ocaml 中:
type 'a mylist = | Nil | Cons of 'a * 'a mylist
Cons : ('a * 'a mylist) -> 'a mylist
UnCons : 'a …Run Code Online (Sandbox Code Playgroud) 我正在构建一个 Rust 应用程序,当应用程序运行时,我想设置一个操作系统级别标志(ENV 变量)说..MY_CUSTOM_APP_RUNNING=true并在应用程序停止时将其设置为 false!
另外,我的应用程序有 3 个不同的功能,我希望我的应用程序能够监听标志的更改。例如,如果我运行export MY_CUSTOM_APP_FEATURE_1=enable,我的应用程序应该侦听此更改。
这2件事可能吗?我应该如何做到这一点?
如果我能为所有操作系统都做到这一点,那就太好了!
我正在通过Make a Lisp来学习 Rust。
\n作为评估器步骤的一部分,我需要创建一个将字符串(或其他内容)映射到函数的关联结构。在我更熟悉的语言(Ruby、Clojure)中,我会简单地在哈希图中定义匿名函数,例如
\n{ :+ (fn [a b] (+ a b))\n :- (fn [a b] (- a b)) } ; etc\nRun Code Online (Sandbox Code Playgroud)\n在 Rust 中,由于类型错误,这是不可能的expected closure, found a different closure。
let repl_env = HashMap::new();\n\nrepl_env.insert("+", |a, b| a + b);\nrepl_env.insert("-", |a, b| a - b); // expected closure, found a different closure\nRun Code Online (Sandbox Code Playgroud)\n我想这里发生的事情是
\nHashMap编译器正在推断as中的类型<String, WhateverTypeTheFirstClosureIs>no two closures, even if identical, have the …我一直认为num * 0.5f和num / 2.0f是等价的,因为我认为编译器足够聪明,可以优化除法。所以今天我决定测试一下这个理论,但我发现的结果却难住了我。
给出以下示例代码:
float mul(float num) {
return num * 0.5f;
}
float div(float num) {
return num / 2.0f;
}
Run Code Online (Sandbox Code Playgroud)
x86-64 clang 和 gcc 都会生成以下汇编输出:
mul(float):
push rbp
mov rbp, rsp
movss DWORD PTR [rbp-4], xmm0
movss xmm1, DWORD PTR [rbp-4]
movss xmm0, DWORD PTR .LC0[rip]
mulss xmm0, xmm1
pop rbp
ret
div(float):
push rbp
mov rbp, rsp
movss DWORD PTR [rbp-4], xmm0
movss xmm0, DWORD PTR [rbp-4]
movss xmm1, DWORD PTR …Run Code Online (Sandbox Code Playgroud)