在这里的代码中
trait Foo {
type Output;
fn foo(self) -> Self::Output;
}
impl<'a> Foo for &'a () {
type Output = &'a ();
fn foo(self) -> Self::Output {
self
}
}
fn func<F: Foo>(f: F) -> F::Output {
f.foo()
}
fn func2<'a>(f: &'a ()) -> &'a () {
func::<&'a ()>(f)
}
fn has_hrl<F: Fn(&()) -> &()>(f: F) {}
fn main() {
//has_hrl(func); // FAILS
has_hrl(func2);
has_hrl(|x| func(x));
}
Run Code Online (Sandbox Code Playgroud)
我们想做has_hrl(func)
,但Rust只接受关闭has_hrl(|x| func(x))
.这是为什么?因为它适用于类似的具体类型func2
,但不适用于泛型类型.
我有一个函数,它接受3个函数并组合它们来改变列表参数.
例如,测试用例调用将是:chain init tail reverse"Haskell!" 输出应该是lleksa
我尝试过几种不同的方式来解决这个问题,包括使用该map
功能,但我一直遇到关联问题.所以我做了
chain :: Ord a => [a] -> a
chain f g h x = f.g.h$x
Run Code Online (Sandbox Code Playgroud)
而错误是 Couldn't match expected type [t0 -> t1 -> t2 -> a0]
例如,当我直接在GHCi中输入问题时,就像写作一样,init.tail.reverse$"Haskell!"
它可以正常工作
有没有办法包含三个函数参数?我在例子中只看到过两个.
这是我的功能.它检查正值,将它们更改为1并对它们求和.
countPositive :: [Integer] -> Integer
countPositive xs = foldr (+) 0 $ map (^0) (filter (>0) xs)
Run Code Online (Sandbox Code Playgroud)
是否有一个更好的策略来计算正值而不使用length
,只是foldr
,map
和filter
?
我正在寻找一些在空间中使用2D和3D点或方向的小型库
(矢量/矩阵感中的矢量,而不是Rust的Vec
).
Rust不会在这里施加规则,所以你可以创建一个浮点元组,或者一个struct
带有x, y, z
成员的新元组.或单个data: [f64; 3]
成员.
我想定义一个类型,而不是在这里使用的原因[f64; 3]
,就是这样我就可以宣布方法,例如length
,normalized
也Add
,Sub
运营商.
什么是宣布小型2D-3D固定大小数据类型的良好基础?
请注意,虽然有很好的现有库,但我想编写自己的库,因为它只需要一些基本操作,我想了解内部发生的事情.
我是Haskell的新手,我在这里得到了一个无限循环,但我不知道为什么.
module Main where
pow :: Int -> Int -> Int
pow x 0 = 1
pow x y = x * pow x y-1
main :: IO ()
main = print( pow 2 3 )
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我已经按照指南创建了一个使用以下数据类型的二叉搜索树:
data BinarySearchTree a = EmptyTree | TreeNode a (BinarySearchTree a) (BinarySearchTree a) deriving (Show, Read, Eq)
Run Code Online (Sandbox Code Playgroud)
我是否正确地说'TreeNode'正在使用递归,即创建自己数据类型的2个元素'(BinarySearchTree a)(BinarySearchTree a)'?
我从来没有见过像这样的数据类型,任何简短的解释都会很棒!
pub type WINDOW = *mut i8;
Run Code Online (Sandbox Code Playgroud)
一个用法WINDOW
:
pub fn newwin(_:c_int,_:c_int,_:c_int,_:c_int) -> WINDOW;
Run Code Online (Sandbox Code Playgroud)
// 1:
typedef struct _win_st WINDOW;
// 2:
struct _win_st {
/* lots of fields... */
};
// 3:
(WINDOW *) newwin (int,int,int,int);
Run Code Online (Sandbox Code Playgroud)
为什么是这种类型的WINDOW
*mut i8
?
我正在读它作为指向C的指针char
,这显然是不正确的.i8
如果你没有在Rust中实现C结构,那么最好简单地说一个指针是类型的吗?这根本不重要吗?
我的Rust代码中有这个泛型函数:
fn test<T>(text: &str) -> T {
text.parse::<T>()
}
Run Code Online (Sandbox Code Playgroud)
这个想法是调用者会做的事情
test::<u64>("2313");
Run Code Online (Sandbox Code Playgroud)
但编译失败了这条消息
error: the trait `core::str::FromStr` is not implemented for the type `T` [E0277]
Run Code Online (Sandbox Code Playgroud)
我昨天刚开始学习Rust,所以这可能是一个非常基本的问题,我找不到答案.
我是一个Rust新手并试图读取两个数字并计算他们的商:
use std::io;
enum Option<T> {
None,
Some(T),
}
fn safe_div(n: i32, d: i32) -> Option<i32> {
if d == 0 {
return None;
}
return Some(n / d);
}
fn main() {
println!("Please input your numerator.");
let mut numerator = String::new();
io::stdin()
.read_line(&mut numerator)
.expect("Failed to read line");
println!("Please input your denominator.");
let mut denominator = String::new();
io::stdin()
.read_line(&mut denominator)
.expect("Failed to read line");
match safe_div(numerator, denominator) {
None => println!("Can't divide by zero!"),
Some(v) => println!("Quotient is {}", …
Run Code Online (Sandbox Code Playgroud) 我正在学习子结构类型系统,Rust是一个很好的例子。
数组在Rust中是可变的,可以访问多次,而不是只能访问一次。“值读取”,“参考读取”和“可变参考读取”之间有什么区别?我编写了如下程序,但出现了一些错误。
fn main() {
let xs: [i32; 5] = [1, 2, 3, 4, 5];
println!("first element of the array: {}", xs[1]);
println!("first element of the array: {}", &xs[1]);
println!("first element of the array: {}", &mut xs[1]);
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
fn main() {
let xs: [i32; 5] = [1, 2, 3, 4, 5];
println!("first element of the array: {}", xs[1]);
println!("first element of the array: {}", &xs[1]);
println!("first element of the array: {}", &mut xs[1]);
}
Run Code Online (Sandbox Code Playgroud) 请考虑以下示例:
use std::env;
use std::path::Path;
fn main() {
let args: Vec<_> = env::args().collect();
let out_path: String = args[2];
let _path = Path::new(out_path);
}
Run Code Online (Sandbox Code Playgroud)
这是我在编译时遇到的错误:
error[E0308]: mismatched types
--> main.rs:8:27
|
8 | let _path = Path::new(out_path);
| ^^^^^^^^
| |
| expected reference, found struct `std::string::String`
| help: consider borrowing here: `&out_path`
|
= note: expected type `&_`
found type `std::string::String`
Run Code Online (Sandbox Code Playgroud)
现在,如果我按照编译器的建议,我得到这个:
error[E0507]: cannot move out of indexed content
--> main.rs:7:28
|
7 | let out_path: String = args[2];
| …
Run Code Online (Sandbox Code Playgroud)