wrestling with Rust

版主: hci

回复
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13243
注册时间: 2022年 7月 26日 00:35

#1 wrestling with Rust

帖子 TheMatrix楼主 »

这两天在玩Rust。不容易。我主要是没有C++的基础,什么unique_ptr,shared_ptr,都是我不玩C++以后才出来的。没用过。Rust看起来很多概念就是为了实现这两个smart pointer。

发现强类型一个不好的地方:就是对高阶函数不友好。因为函数的类型是很长的,比如:
fn (Vec<String>, Vec<String>) --> Vec<String>

这就是一个二元函数的类型声明。这还只是一个一阶函数,二阶函数就更长了。所以不友好。

Rust里面函数类型还有变体,什么Box,dyn,为了实现closure。我觉得概念不是很直接。closure应该和对应的函数是同一个类型,我觉得这样才直接。不能实现的话,是不是说明还没有设计完美?python里面似乎不用区分closure和函数。

+2.00 积分 [版主 hci 发放的奖励]

标签/Tags:
magagop
见习点评
见习点评
帖子互动: 113
帖子: 1654
注册时间: 2024年 12月 5日 17:35

#2 Re: wrestling with Rust

帖子 magagop »

不理解,有C基礎為什麼不直接上C++?
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13243
注册时间: 2022年 7月 26日 00:35

#3 Re: wrestling with Rust

帖子 TheMatrix楼主 »

magagop 写了: 2025年 4月 30日 11:01 不理解,有C基礎為什麼不直接上C++?
主要为了学一个面向未来的语言。
magagop
见习点评
见习点评
帖子互动: 113
帖子: 1654
注册时间: 2024年 12月 5日 17:35

#4 Re: wrestling with Rust

帖子 magagop »

TheMatrix 写了: 2025年 4月 30日 19:21 主要为了学一个面向未来的语言。
C++變化挺大的,適合熟悉C和Python的進階。C++越來越像Python了。Rust我感覺跨度太大,學不習慣。
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13243
注册时间: 2022年 7月 26日 00:35

#5 Re: wrestling with Rust

帖子 TheMatrix楼主 »

magagop 写了: 2025年 4月 30日 19:25 C++變化挺大的,適合熟悉C和Python的進階。C++越來越像Python了。Rust我感覺跨度太大,學不習慣。
对我来说跨度一样大,所以选面向未来的。其实我就是学着玩,并不真需要。
squirrelrun
小有名气
小有名气
帖子互动: 5
帖子: 37
注册时间: 2023年 3月 25日 14:40

#6 Re: wrestling with Rust

帖子 squirrelrun »

TheMatrix 写了: 2025年 4月 30日 20:38 对我来说跨度一样大,所以选面向未来的。其实我就是学着玩,并不真需要。
感觉Mojo语言更有未来。
上次由 hci 在 2025年 5月 3日 20:20 修改。
原因: 未提供修改原因
头像
hci(海螺子)
论坛支柱
论坛支柱
帖子互动: 465
帖子: 9926
注册时间: 2022年 7月 22日 15:29

#7 Re: wrestling with Rust

帖子 hci(海螺子) »

Rust不行,brower checker搞得连double linked list都实现不了,这种表达力上的限制太过分了。

Zig也不行,那个作者有些非常奇葩的口味,比如没用上的变量是个编译错误,这也太过分了。

看了一圈,觉得可能还是逃不掉C++。

当然了,最好的,还是多层混合编程的理念。最下层用C,中间层用一个GC语言,最上层动态类型语言。这其实也是业界逐步进化并皈依到的模式。
头像
hci(海螺子)
论坛支柱
论坛支柱
帖子互动: 465
帖子: 9926
注册时间: 2022年 7月 22日 15:29

#8 Re: wrestling with Rust

帖子 hci(海螺子) »

不开源的语言都没有前途。

squirrelrun 写了: 2025年 5月 2日 09:20 感觉Mojo语言更有未来。
头像
TheMatrix楼主
论坛支柱
论坛支柱
2024年度优秀版主
TheMatrix 的博客
帖子互动: 264
帖子: 13243
注册时间: 2022年 7月 26日 00:35

#9 Re: wrestling with Rust

帖子 TheMatrix楼主 »

TheMatrix 写了: 2025年 4月 29日 17:34 这两天在玩Rust。不容易。我主要是没有C++的基础,什么unique_ptr,shared_ptr,都是我不玩C++以后才出来的。没用过。Rust看起来很多概念就是为了实现这两个smart pointer。

发现强类型一个不好的地方:就是对高阶函数不友好。因为函数的类型是很长的,比如:
fn (Vec<String>, Vec<String>) --> Vec<String>

这就是一个二元函数的类型声明。这还只是一个一阶函数,二阶函数就更长了。所以不友好。

Rust里面函数类型还有变体,什么Box,dyn,为了实现closure。我觉得概念不是很直接。closure应该和对应的函数是同一个类型,我觉得这样才直接。不能实现的话,是不是说明还没有设计完美?python里面似乎不用区分closure和函数。
终于把python写的SQL parser port到Rust了。绝对的wrestling,战了好几天。

use std::any::Any;
use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};

type State = u32;
type Token = (String, String);
type Data = Vec<Arc<dyn Any + Send + Sync>>;
type BNF = Arc<dyn Fn(State) -> (State, Data) + Send + Sync>;
type PostProcessor = Arc<dyn Fn((State, Data)) -> (State, Data) + Send + Sync>;
type GoodChecker = Arc<dyn Fn(&Data) -> bool + Send + Sync>;

static TOKENS: Lazy<Mutex<Data>> = Lazy::new(|| Mutex::new(vec![]));

这个类型声明,没有chatgpt我是搞不定。

有了chatgpt,递归那里也是怎么都调不通。我以为是死循环,后来发现可能是Mutex递归lock住了。现在我也没明白是怎么回事。

还有一个generic的版本:

use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};

type State = u32;
type Token = (String, String);
type BNF<T> = Arc<dyn Fn(State) -> (State, Vec<T>) + Send + Sync>;
type PostProcessor<T> = Arc<dyn Fn((State, Vec<T>)) -> (State, Vec<T>) + Send + Sync>;
type GoodChecker<T> = Arc<dyn Fn(&Vec<T>) -> bool + Send + Sync>;

static CHARS: Lazy<Mutex<Vec<char>>> = Lazy::new(|| Mutex::new(vec![]));
static TOKENS: Lazy<Mutex<Vec<Token>>> = Lazy::new(|| Mutex::new(vec![]));
头像
hci(海螺子)
论坛支柱
论坛支柱
帖子互动: 465
帖子: 9926
注册时间: 2022年 7月 22日 15:29

#10 Re: wrestling with Rust

帖子 hci(海螺子) »

太难看了
TheMatrix 写了: 2025年 5月 12日 20:57 终于把python写的SQL parser port到Rust了。绝对的wrestling,战了好几天。

use std::any::Any;
use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};

type State = u32;
type Token = (String, String);
type Data = Vec<Arc<dyn Any + Send + Sync>>;
type BNF = Arc<dyn Fn(State) -> (State, Data) + Send + Sync>;
type PostProcessor = Arc<dyn Fn((State, Data)) -> (State, Data) + Send + Sync>;
type GoodChecker = Arc<dyn Fn(&Data) -> bool + Send + Sync>;

static TOKENS: Lazy<Mutex<Data>> = Lazy::new(|| Mutex::new(vec![]));

这个类型声明,没有chatgpt我是搞不定。

有了chatgpt,递归那里也是怎么都调不通。我以为是死循环,后来发现可能是Mutex递归lock住了。现在我也没明白是怎么回事。

还有一个generic的版本:

use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};

type State = u32;
type Token = (String, String);
type BNF<T> = Arc<dyn Fn(State) -> (State, Vec<T>) + Send + Sync>;
type PostProcessor<T> = Arc<dyn Fn((State, Vec<T>)) -> (State, Vec<T>) + Send + Sync>;
type GoodChecker<T> = Arc<dyn Fn(&Vec<T>) -> bool + Send + Sync>;

static CHARS: Lazy<Mutex<Vec<char>>> = Lazy::new(|| Mutex::new(vec![]));
static TOKENS: Lazy<Mutex<Vec<Token>>> = Lazy::new(|| Mutex::new(vec![]));
回复

回到 “葵花宝典(Programming)”