考虑这个 Haskell 程序
module RecursiveArray where
import Data.Array ( (!), listArray, Array )
goodArray :: Array Int Int
goodArray = listArray (0, 1) (go 0)
where
go x = x : go ((goodArray ! x) + 1)
badArray :: Array Int Int
badArray = listArray (0, 1) (go 0)
where
go !x = x : go ((badArray ! x) + 1)
main = do
print goodArray
print badArray
Run Code Online (Sandbox Code Playgroud)
哪个将打印
> runghc "RecursiveArray.hs"
array (0,1) [(0,0),(1,0)]
array <program stalls here>
Run Code Online (Sandbox Code Playgroud)
我需要一些帮助来理解这里发生的事情。人们可以使用等式推理来理解正在发生的事情吗?数组内部表示相关吗? …
我无法弄清楚是什么导致了这个引用错误:
error[E0515]: cannot return value referencing local variable `s1`
--> src/regexp/mod.rs:538:9
|
528 | match s1.step() {
| --------- `s1` is borrowed here
...
538 | steps
| ^^^^^ returns a value referencing data owned by the current function
For more information about this error, try `rustc --explain E0515`.
error: could not compile `regexp` due to previous error
Run Code Online (Sandbox Code Playgroud)
我已经创建了 CharsStep 结构克隆和复制,并且在两次推送中我都尝试推送对象的克隆并进行分配以获取副本并推送它。我以为我开始理解内存模型了,但这个模型让我陷入困境。这里借的是什么?在添加到步骤向量之前,是否所有引用都已复制(多次!)?
我查看了建议的参考,它只是解释了我所知道的,即我只能返回值,而不是对本地值的引用,但由于它们是克隆的,所以我认为它们没问题。
pub trait Walker {}
// used to hold the state at each step in the search walk, and …Run Code Online (Sandbox Code Playgroud) 如何u128将 a 转换为 s 数组u64。当将 a 传递u128给采用任意精度 int 的 API 时,该 API 需要 3 个参数
LLVMValueRef LLVMConstIntOfArbitraryPrecision (
LLVMTypeRef IntTy,
unsigned NumWords,
const uint64_t Words[]
)
Run Code Online (Sandbox Code Playgroud)
前两个参数已知(IntTy=LLVMInt128Type和NumWords= 2)。第三个参数需要一个 s 数组uint64_t。提供的u128需要转换为u64的数组。从rust 文档来看,它似乎u128可以转换为字节数组,例如
let buff: [u8; 16] = u128_val.to_ne_bytes();
let Words: [u64; 2] = ?? // What to do here?
Run Code Online (Sandbox Code Playgroud)
如何buff转换为数组Words?另外,如何处理字节序。为简单起见,代码生成器和 API 将在具有相同字节序的机器上运行。
我写了这段代码:
\ncalculIOTest :: IO ()\ncalculIOTest = do\n putStrLn "Give me two numbers"\n l1 <- getLine\n let x1 = read l1 \n l2 <- getLine\n let x2 = read l2 \n print (x1 + x2)\nRun Code Online (Sandbox Code Playgroud)\n我想要接受两个数字并返回总和的代码。如果我用两个整数测试我的函数,它可以工作,但如果我放置一个浮点数,则会出现错误问题:
\ncalculIOTest :: IO ()\ncalculIOTest = do\n putStrLn "Give me two numbers"\n l1 <- getLine\n let x1 = read l1 \n l2 <- getLine\n let x2 = read l2 \n print (x1 + x2)\nRun Code Online (Sandbox Code Playgroud)\n我本来可以用字符串和数字来理解,但在这里我很难理解导致问题的原因。
\n我试图回顾一下 read 是如何工作的,如果我这样做的话:
\n***Exception: Prelude.read: …Run Code Online (Sandbox Code Playgroud) 我目前正在阅读dua-cli的源代码,并遇到一个需要满足 Fn 特征的参数的函数调用。代码是:
type WalkDir = jwalk::WalkDirGeneric<((), Option<Result<std::fs::Metadata, jwalk::Error>>)>;
impl WalkOptions {
pub(crate) fn iter_from_path(&self, root: &Path, root_device_id: u64) -> WalkDir {
WalkDir::new(root)
.follow_links(false)
.sort(match self.sorting {
TraversalSorting::None => false,
TraversalSorting::AlphabeticalByFileName => true,
})
.skip_hidden(false)
.process_read_dir({
// I don't understand the following two lines
let ignore_dirs = self.ignore_dirs.clone();
let cross_filesystems = self.cross_filesystems;
move |_, _, _, dir_entry_results| {
// Closure logic here
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
process_read_dir 函数签名是
pub fn process_read_dir<F>(self, process_by: F) -> Selfwhere
F: Fn(Option<usize>, &Path, &mut …Run Code Online (Sandbox Code Playgroud) 我编写了一个程序,利用库Hashset中的unordered-containers。它在我的 GHC 9.2.4 计算机上运行良好。由于某些原因,我正尝试将其移植到标题中提到的 GHC 8.8.4。它主要归结为添加一些语言扩展。
不过我遇到了一个奇怪的问题。
\n我有一个功能powerset :: Hashable a => Set a -> [Set a]。
编译时我收到以下错误:
\nCould not deduce (hashable-1.3.0.0:Data.Hashable.Class.Hashable\n a)\n arising from a use of \xe2\x80\x98makeSubset\xe2\x80\x99\n from the context: Hashable a\n bound by the type signature for:\n powerset :: forall a. Hashable a => Set a -> [Set a]\n at Automaton.hs:48:1-45\n Possible fix:\n add (hashable-1.3.0.0:Data.Hashable.Class.Hashable\n a) to the context of\n the type signature for:\n powerset :: forall …Run Code Online (Sandbox Code Playgroud) 我一直在学习 Rust,并且一直在尝试学习借用检查器的工作原理,但我遇到了这两个例子,我不明白为什么只有其中一个被认为是借用的:
fn main() {
let mut x = String::from("aa ab");
let y = first_word(&x);
x.clear(); //Error cannot borrow X
println!("{y}");
}
//Returns an i32 reference
fn first_word(s: &String) -> &i32 {
return &32;
}
Run Code Online (Sandbox Code Playgroud)
fn main() {
let mut x = String::from("aa ab");
let y = first_word(&x);
x.clear(); //Everything is fine
println!("{y}");
}
//Returns an i32
fn first_word(s: &String) -> i32 {
return 32;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么只有第二个有效吗?
我尝试使用迭代器进行“正确的”Rust 版本的冒泡排序。明显的代码包含以下几行:
let mut vi = v.windows(2);
for mut i in vi {
if i[0] > i[1] {
i.swap(0, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到了错误
let mut vi = v.windows(2);
for mut i in vi {
if i[0] > i[1] {
i.swap(0, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法从迭代器获取对向量元素对的可变引用?
我正在开发一个项目,我需要重复地将已知数量的元素从缓冲区收集到向量中,我当前的代码是:
let mut info: Vec<i32> = buffer.trim().split(" ").collect()
Run Code Online (Sandbox Code Playgroud)
然而,据我所知,我将收到 2 个元素,我希望能够设置向量的容量,例如:
let mut info: Vec<i32>::with_capacity(2) = buffer.trim().split(" ").collect()
Run Code Online (Sandbox Code Playgroud) 我正在阅读一本有关操作系统的开源教科书,这是第一个代码示例(文件名是“cpu.c”):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: cpu <string>\n");
exit(1);
}
char *str = argv[1];
while (1) {
sleep(1);
printf("%s\n", str);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在终端中运行“./cpu 'A'”时,一切正常,我可以使用 Ctrl + C 终止它。但是,当我使用
./cpu A & ./cpu B & ./cpu C & ./cpu D &
Run Code Online (Sandbox Code Playgroud)
使程序的多个实例运行,并尝试使用 Ctrl + C,代码继续无限期地运行。我在网上看到 Ctrl + Z 可以工作,但没有帮助。有没有办法在不关闭终端的情况下结束4个程序的运行?我已经看过这个,但我不相信它能帮助解决我的问题。