下面是一段更复杂的代码片段,其思想是加载一个 SQL 表并设置一个哈希图,其中一个表结构字段作为键,并将结构保留为值(实现细节并不重要,因为代码工作正常但是,如果我克隆String,数据库中的字符串可以是任意长,并且克隆可能会很昂贵)。
以下代码将失败并显示
error[E0382]: use of partially moved value: `foo`
--> src/main.rs:24:35
|
24 | foo_hashmap.insert(foo.a, foo);
| ----- ^^^ value used here after partial move
| |
| value partially moved here
|
= note: partial move occurs because `foo.a` has type `String`, which does not implement the `Copy` trait
For more information about this error, try `rustc --explain E0382`.
Run Code Online (Sandbox Code Playgroud)
error[E0382]: use of partially moved value: `foo`
--> src/main.rs:24:35
|
24 | foo_hashmap.insert(foo.a, foo);
| ----- …Run Code Online (Sandbox Code Playgroud) 如果我有一个vec<string>我可以用来filter_map处理和消除整体。但有没有相反的选择呢filter?
本质上有一种惯用的方式来做这样的事情 -
word_list.iter().merge_map(|s| s.split(".")).collect()
^this is an imaginary method.
Run Code Online (Sandbox Code Playgroud)
将输入["a","b.c","d"]变成["a","b","c","d"]
例如,为什么不能定义这样的函数 -
fun::a
fun = 1
-- OR
someInt::Int
someInt = 3
fun::Num a => a
fun = someInt
Run Code Online (Sandbox Code Playgroud)
看来这在 Java 中是可能的 -
class App {
public static void main(String[] args) {
System.out.println(new SomeClass().hello().sayHi());
}
}
class SomeClass {
Hi hello() {
A a = new A();
return a;
}
}
interface Hi {
String sayHi();
}
class A implements Hi {
public String sayHi() {
return "Hi from A";
}
}
class B implements Hi {
public String sayHi() …Run Code Online (Sandbox Code Playgroud) 考虑这个 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) 我在 Haskell 中有这个蛋糕烘焙状态机:
\n------------------------------------------------------------------------------\n-- Ex 3: a state machine for baking a cake. The type Event represents\n-- things that can happen while baking a cake. The type State is meant\n-- to represent the states a cake can be in.\n--\n-- Your job is to\n--\n-- * add new states to the State type\n-- * and implement the step function\n--\n-- so that they have the following behaviour:\n--\n-- * Baking starts in the Start state\n-- * A successful cake (reperesented by the Finished …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)
有人可以解释为什么只有第二个有效吗?
我正在尝试根据正在编译应用程序的设备来更改应用程序的图标(我正在使用 crate 加载image) 。不幸的是,当尝试运行代码时:
let (icon_rgba, icon_width, icon_height) = {
let icon = if cfg!(target_os = "macos") {
include_bytes!("../assets/icon-macos.png")
} else {
include_bytes!("../assets/icon.png")
};
let image = image::load_from_memory(icon)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error[E0308]: `if` and `else` have incompatible types
--> src/main.rs:6:13
|
3 | let icon = if cfg!(target_os = "macos") {
| ____________________-
4 | | include_bytes!("../assets/icon-macos.png")
| | ------------------------------- …Run Code Online (Sandbox Code Playgroud)