小编lje*_*drz的帖子

更高级别的生命周期和泛型不能很好地发挥

在这里的代码中

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,但不适用于泛型类型.

closures traits lifetime rust

1
推荐指数
1
解决办法
270
查看次数

Haskell中的函数组合

我有一个函数,它接受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!"它可以正常工作

有没有办法包含三个函数参数?我在例子中只看到过两个.

haskell

0
推荐指数
1
解决办法
486
查看次数

计算正值

这是我的功能.它检查正值,将它们更改为1并对它们求和.

countPositive :: [Integer] -> Integer
countPositive xs = foldr (+) 0 $ map (^0) (filter (>0) xs)
Run Code Online (Sandbox Code Playgroud)

是否有一个更好的策略来计算正值而不使用length,只是foldr,mapfilter

haskell list

0
推荐指数
2
解决办法
1225
查看次数

在Rust中声明2D,3D矢量类型的惯用方法?

我正在寻找一些在空间中使用2D和3D点或方向的小型库
(矢量/矩阵感中的矢量,而不是Rust的Vec).

Rust不会在这里施加规则,所以你可以创建一个浮点元组,或者一个struct带有x, y, z成员的新元组.或单个data: [f64; 3]成员.

我想定义一个类型,而不是在这里使用的原因[f64; 3],就是这样我就可以宣布方法,例如length,normalizedAdd,Sub运营商.

什么是宣布小型2D-3D固定大小数据类型的良好基础?


请注意,虽然有很好的现有库,但我想编写自己的库,因为它只需要一些基本操作,我想了解内部发生的事情.

types linear-algebra rust

0
推荐指数
1
解决办法
1001
查看次数

pow功能的无限循环

我是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)

有任何想法吗?

haskell function operator-precedence

0
推荐指数
1
解决办法
97
查看次数

Haskell - 使用递归的代数数据类型?

我已经按照指南创建了一个使用以下数据类型的二叉搜索树:

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)'?

我从来没有见过像这样的数据类型,任何简短的解释都会很棒!

haskell types recursive-datastructures algebraic-data-types

0
推荐指数
1
解决办法
193
查看次数

为什么*mut i8用于ncurses-rs中的WINDOW类型?

在ncurses-rs包中给出这个定义:

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)

和实施,在ncurses的C库(1,2,3):

// 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结构,那么最好简单地说一个指针是类型的吗?这根本不重要吗?

types rust

0
推荐指数
1
解决办法
93
查看次数

解析数字的通用函数失败,"FromStr未实现"

我的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,所以这可能是一个非常基本的问题,我找不到答案.

generics rust

0
推荐指数
1
解决办法
947
查看次数

读取和计算商数时出错

我是一个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)

types rust

0
推荐指数
1
解决办法
145
查看次数

Rust如何实现数组索引?

我正在学习子结构类型系统,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)

arrays mutability indices rust

0
推荐指数
1
解决办法
860
查看次数

为什么我不能将env :: Args中的String传递给Path :: new?

请考虑以下示例:

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)

rust

0
推荐指数
1
解决办法
143
查看次数