导航:首页 > 源码编译 > rust加快编译速度

rust加快编译速度

发布时间:2023-07-04 07:58:55

1. rust性能到底有多好

这个和c++大同小异。 因为把大量的运行时放到了编译时。 只不过编译器优化不够还达不到c++性能。
其实rust和c++的关系很想scala和java的关系。 rust和c++很多概念甚至库都是通用的,抽象化方式,比如泛性,多态,可变性,拷贝构造,移动构造,都是一样的。 你要是写过c++再来写rust,就会吐槽原来这个功能也有啊...

运行速度上rust c++ java都是大同小异,io处理上 java甚至比rust还要快。内存消耗rust和c++是一个级别的,都非常低。 另外很多人写rust喜欢用arc cellref这些只能指针, 其实是给这些变量增加了动态性会导致额外开销,所以这些特性用多了后,rust性能也不会太高:

2. 哪种高级编程语言的执行速度最快

回答时间2019年下半年
仅限高级语言
排名由前到后
性能 C Rust C++ Ada Java Pascal Chapel Lisp Ocaml Fortran Swift Haskell CSharp(C#) Go Dart F# Javascript Racket

时间 C Rust C++ Ada Java Chapel Go Pascal Ocaml C# Lisp Haskell Swift Fortran F# Javascript Dart Racket

内存占用 Pascal Go C Fortran C++ Ada Rust Lisp Haskell PHP Swift Python Ocmal C# Hack Racket

性能 对CPU资源消耗程度 越少越好
时间 运行同样代码任务所需时间 越短越好
内存占用 对内存的消耗 越少越好

C Rust C++ Ada 都是强类型的静态语言,性能非常接近,java可以说的带runtime的最快的语言。目前这四门顶级性能高级语言中最设计先进的是Rust,由于rust为了解决C系遗留安全问题,引入了ownership lifetime reference,因此最难入门的也是Rust。C C++ 入门较容易,但设计复杂的大型项目时C和C++将会变得困难,特别是C。建议学习路线 C>C++>Rust。

3. Rust语言的最新版本说明

Mozilla在2014年10月宣布发布Rust编译器和工具的0.12版。
0.12版有1900多项变化和bug修正,其中主要包括:重写了入门文档(现在叫Rust Guide);继续提高了包管理器Cargo等。
在2015年1月,发布了Rust-1.0.0-alpha版本。
2015年5月15日,Rust编程语言核心团队正式宣布发布Rust 1.0版本。
2015年6月25日,Rust发布了1.1版本。同时发布1.2测试版本 编译速度在1.1版本的基础上再提升30% 并行编译又能用了,默认未开启,如果开启,还能提升33%的编译速度(数据来自4核编译rustc) Cargo性能提升(编译Servo时启动速度提升10倍),可在多个package之间共享依赖包缓存 初步支持MSVC(Microsoft Visual C),以后Windows环境中不需要MinGW/MySYS/GCC了 Rust 1.2 稳定版 将在六周之后发布,届时还将一并发布 1.3 测试版

4. 腐蚀RUST游戏指令和服务器指令一览

腐蚀 RUST游戏指令和服务器指令一览,在服务器中,管理员也可以利用指令来管理游戏中不正当行为。下面就给大家带来腐蚀RUST基本指令及服务器指令大全,以供玩家们参考。

基本指令

(以下在聊天框内输入)

/msg【message a specified player(私信一个玩家)】

/me【Puts your text into a purple color(你的 文字 将以紫色发出)】

(以下在控制台内输入,按F1)

grass.on true/false 【Enables or disables grass; Improves FPS for some.(打开/关闭草地,可提高FPS)】

grass.displacement 【true/false Enables or disables grass displacements.(打开关闭草地 移动 )】

terrain.idleinterval 0-100 【Sets how often to draw unseen terrain; setting to 0 will disable.(远景更新平率,0为不允许)】

gui.show 【Turns the UI on.(显示用户界面)】

gui.hide 【Turns the UI off.(关闭用户界面)】

gui.show_branding 【Turns the branding UI in top-right corner on.(显示右上角的标识)】

gui.hide_branding 【Turns the branding UI in top-right corner off.(隐藏右上角的标识)】

net.connect "Server IP" 【Connect to a direct server IP.(连接服务器IP,Server IP出填写服务器的IP)】

net.disconnect 【Disconnects from a server.(断开服务器连接)】

net.reconnect 【Reconnect to the last server you were on.(重新连接上一个服务器)】

censor.nudity false 【Disabled censorship.(关闭裸体)】

suicide 【Kills your character allowing for a respawn.(自杀)】

quit 【Quits the game.(退出游戏)】

服务器指令

rcon.login "password" 【Use your 'Password' to login into Rcon via ingame console (F1)。(使用控制台登录服务器)】

status 【See how many players are online on server.(查看在线玩家数量)】

notice.popupall "message" 【Pops up a message for all players.(发送服务器公告,在每个玩家屏幕上弹出)】

find * 【Lists available console commands.(列出控制台指令)】

kick "player" 【Kicks player from the server.(踢出某个玩家)】

ban "player" or "steamid" 【Bans player. Doesn't kick him though.(封掉一个玩家)】

banid "steamid" "reason" 【Bans a steamid from the server.(封掉一个玩家的steamID,reason处可写理由,挂狗和无(B)素(I)质(U)狗去死吧)】

unbanall 【Unbans all players.(解封所有玩家)】

truth.enforce true/false 【Server kicks people automatically when they are doing "weird" things.(服务器自动踢出做奇怪事情的人,如卡BUG)】

save.all 【Saves world map and player inventory(服务器保存当前地图和玩家信息)。】

say [message] 【Sends a message to the person/s in-game globally.(以服务器身份说话)】

inv.giveplayer "player" "item" "amount" 【Gives 'Player' the 'Item'. Full name and Item name required. List of Items.(给玩家东西,“玩家”“物品”“数量”)】

inv.giveall "item" "amount" 【Gives all players 'Item'. Full Item name required. List of Items.(给所有人东西,“玩家”“物品”“数量”,比如节日的时候,可以作为惊喜,没人给一把枪什么的)】

dmg.godmode true/false 【Gives all logged in admins godmode.(开启/关闭所有人 创造 模式)】

crafting.cancel 【Cancels every single crafting job in progress for everyone.(取消制作任何东西)】

crafting.instant true/false 【Sets crafting to be instant for everyone.(开启/关闭瞬间制作)】

crafting.instant_admins true/false 【Sets crafting to be instant for logged in admins only.(开启/关闭管理员瞬间制作)】

crafting.timescale "amount" 【Sets the timescale of crafting to 'amount' (1 = default, 0.5 = half time)。(设置制作物品时间的速度,1为默认,0.5为一半时间)】

airdrop.drop 【Starts an airdrop.(进行一次物品空降)】

airdrop.min_players "amount" 【Starts airdrops only when minimum X players are online.(开始物品空降,当在线人数至少“X”人的时候)】

vehicle.spawn 【Spawns a car at your current position.( 放置 一辆车,在你当前位置,管理装B专用)】

server.hostname 【Sets a hostname.(设置服务器名称)】

server.clienttimeout "time"【Sets the time until someone times out. Good to fight item glitchers. (Default 2 minutes)(设置超时时长,默认为2分钟)】

server.pvp true/false 【Sets PVP on or off.(开启PVP)】

server.maxplayers "amount" 【Sets maximum amount of server slots.(设置最大玩家数量)】

sleepers.on true/false 【Sets sleepers on or off.(是否允许睡眠模式)】

env.time "amount" 【Sets the time of day to a specified value.(设置天的时间)】

falldamage.enabled true/false 【Turns fall damage on or off.(开启/关闭掉落伤害)】

RUST++ MOD

(以下在聊天框内输入)

基本命令

/share playername 【shares your doors with a player(共享你的门给一个玩家)】

/unshare playername 【unshares your doors with a player(解除对一个玩家的门共享)】

/help 【Shows commands(显示所有指令)】

/pm "playername" "message" 【private messages a player.(私聊一个玩家,名字必须写全)】

/r message here 【quick reply to last PM(快速回复上一个私聊你的玩家)】

/history 【Shows chat history, last 6 messages by default(显示前6个聊天内容)】

/players 【Shows online players(显示当前在线玩家)】

/location 【Shows the coordinates of the player(显示玩家的当前坐标)】

/ping 【shows latency between client and server(显示服务器的延迟)】

/starter 【gives a configurable starter kit(给予初始装备)】

/friends 【shows your friends list(显示朋友列表)】

/addfriend playername 【adds a player to your friends list(添加一个玩家为好友)】

/unfriend playername 【removes a friend(移除一个好友)】

/about 【shows server mod version(显示服务器版本)】

管理员命令

/announce message here 【ADMIN ONLY - announces a message to the server(发送服务器公告)】

/loadout 【ADMIN ONLY - spawns you an admin loadout set in the config file(生成一个管理操作记录在设置文件夹下)

RustEssentials MOD

(以下在聊天框内输入)

/access {on} 【(Gives the sender access to all doors,给予你打开所有门的权限)】

/access {off} 【(Revokes access to all doors from the sender,移除你打开所有门的权限)】

/airdrop 【(Spawns an airdrop with a random drop location,设置一次随地地点的空降)】

/airdrop【(Spawns an airdrop with a drop location at the specified player,设置一次空降给某个玩家)】

/ban【(Bans player with reason: "Banned by a(n)",封掉一个玩家)】

/ban[reason] 【(Bans player with reason,封掉一个玩家和愿意)】

/chan {g} 【(Joins the global chat,加入全球聊天屏道)】

/chan {global} 【(Joins the global chat,同上)】

/chan {d} 【(Joins the direct chat,加入一个特定聊天屏道)】

/chan {direct} 【(Joins the direct chat,同上)】

/give【(Gives the item to that player,给一个玩家指定物品)】

/give[amount] 【(Gives the amount of the item to that player,给一个玩家指定物品和数量)】

/give[item id] 【(Gives 1 of the item with the corresponding id to that player,给一个玩家指定的物品,这里用的是物品ID)】

/give[item id] [amount] 【(Gives the amount of the item with the corresponding id to that player,给一个玩家指定的物品和数量,这里用的是物品ID)】

/god【(Gives the specified player god mode,给指定玩家上帝模式)】

/heal *player name* 【(Heals the designated player,恢复指定玩家的血)】

/help 【(Returns available commands for your current rank,显示指令)】

/help [command without /] 【(Returns the documentation and syntax for the specified command,显示已经排除外的指令,without/后面填上你不想显示的指令)】

/history {1-50} 【(Returns the the last # lines of the chat history,查看聊天记录,1-50里面填你想查看前面多少条记录)】

/i【(Gives the item to you,给自己指定物品)】

/i[amount] 【(Gives the amount of the item to you,给自己指定物品和数量)】

/i [item id] 【(Gives 1 of the item with the corresponding id to you,给自己指定物品,这里用的是物品ID)】

/i [item id] [amount] 【(Gives the amount of the item with the corresponding id to you,给自己指定物品和数量,这里用的是物品ID)

/join 【(Emulates the joining of yourself, 模拟 自己加入游戏)】

/join【(Emulates the joining of a fake player,加入一个机器人到游戏中)】

/kick【(Kick player with reason: "Kicked by a(n)",踢出一个玩家)】

/kick[reason] 【(Kick player with reason,踢出一个玩家和愿意)】

/kickall 【(Kicks all users, except for the command executor, out of the server踢出所有玩家,除了输入这条指令者)】

/kill【(Kills the specified player,杀死一个指定玩家)】

/kit [kit name] 【(Gives the user the specified kit if the user has the correct authority level,给一个用户指定套装,当这个玩家有指定的权限的时候)

/kits 【(Lists the kits available to you,列出自己所有能用的套装)】

/leave 【(Emulates the leaving of yourself,模拟自己离开游戏)】

/leave【(Emulates the leaving of a fake player,模拟一个机器人离开游戏)】

/mute【(Mutes the player on global chat,禁言一个玩家)】

/online 【(Returns the amount of players currently connected,显示所有服务器在线玩家)】

/pm*message* 【(Sends a private message to that player,私聊某个玩家)】

/pos 【(Returns the player's position,显示玩家所在的坐标)

/reload {config/whitelist/ranks/commands/kits/motd/bans/all} 【(Reloads the specified file,重新加载指定的文件)】

/save 【(Saves all world data,保存世界数据)】

/say *message* 【(Says a message through the plugin,以服务器身份说话,在公屏上)】

/saypop *message* 【(Says a (!) dropdown message to all clients,弹出一段话,在玩家屏幕上)】

/saypop [icon] *message* 【(Says a dropdown message to all clients with designated icon),以icon的什么弹出一段话,在玩家屏幕上】

/share *player name* 【(Shares ownership of your doors with the designated user,共享门给指定玩家)】

/stop 【(Saves, deactivates, and effectively stops the server,停止服务器并保存)】

/time 【(Returns current time of day,显示当前世界时间)】

/time {0-24} 【(Sets time to a number between 0 and 24,设置当前世界时间)】

/time {day} 【(Sets time to day,设置为白天)】

/time {freeze} 【(Freezes time,冻住当前时间)】

/time {night} 【(Sets time to night,设置为晚上)】

/time {unfreeze} 【(Unfreezes time,解冻当前时间)】

/timescale 【(Returns the speed at which time passes,显示当前时间流逝速度)】

/timescale [#] 【(Sets the speed at which time passes. Recommended between 0 and 1. WARNING: THIS AFFECTS AIRDROPS,设置时间流逝速度,在0和1之间,警告!这个将影响到空降时间)】

/uid 【(Returns your steam UID,显示你的steamID)】

/uid *player name* 【(Returns that user's steam UID,显示指定玩家的steamID)】

/unban【(Unbans the specified player,解封指定玩家)】

/ungod【(Revokes god mode from the specified player,解除指定玩家的上帝模式)】

/unmute【(Unmutes the player on global chat,解除指定玩家的禁言)】

/unshare {all} 【(Revokes ownership of your doors from everyone,解除对所有玩家的门共享)

/unshare *player name*【(Revokes ownership of your doors from the designated user,解除对指定玩家的门共享)

/version 【(Returns the current running version of Rust Essentials,重置Rust Essentials模组的版本)

/whitelist {add} [UID] 【(Adds the specified Steam UID to the whitelist,把指定steamID添加至白名单)】

/whitelist {check} 【(Checks if you're currently on the whitelist,查询白名单)】

/whitelist {kick} 【(Kicks all players that are not whitelisted. This only work if whitelist is enabled,踢出所有在白名单的人,如果白名单已经启动)】

/whitelist {off} 【(Turns whitelist off,关掉白名单)】

/whitelist {on} 【(Turns whitelist on,打开白名单)】

/whitelist {rem} [UID] 【(Removes the specified Steam UID to the whitelist,移除指定steamID从白名单)】

5. Java和Rust在实现多线程编程时的异同

Java的实现
打开Follower.java里的这个函数

这里的Follower.this.invitations就是我们的消息队列,定义是:private LinkedList<Invitation> invitations;LinkedList不是线性安全的集合,需要我们加同步。具体的同步方法就是函数里写的,通过Java常见的用wait,notify和notifyall给对象加锁。
处理并发有wait、notify和notiyall,有兴趣的朋友可以去这里了解一下:http://www.importnew.com/16453.html。Follower就是一个等待leader发送invitation,处理并返回结果的过程。
Leader.java
这么一段代码:

里面就是Leader发送邀请inv,并等待follower返回结果的大概逻辑,通过对消息体加锁,是Java传统的实现多线程并发的方式。还有消费者的消息队列也会加锁,在Java里,有个对象叫LinkedBlockingQueue,是不用加锁就可以put和take的,但在例子里,我们选用了更简单的LinkedList,也是为了表现一下加锁的逻辑。
Rust的实现
Leader的结构为:

Follower的结构为:

对于其他语言转过来的同学,这里的Vec,i32,bool都很好理解,不过里面出现的Arc和Mutex,Sender,Receiver就是新东西了,上面这4个都是Rust标准库的东西,也是这次分享要介绍的重点对象,是这4个东西共同实现了消息的生产,传递和消费。
下面简单介绍一下分别是做什么用的:
Arc<T>实现了sync接口。Sync接口是做什么呢?权威资料是这么说的:当一个类型T实现了Sync,它向编译器表明这个类型在多线程并发时没有导致内存不安全的可能性。
如果看不懂不要紧,我们先看看实际中是怎么用的:

在这个例子里,我们关注这几句:
let data = Arc::new(Mutex::new(vec![1u32, 2, 3]));
let data = data.clone();
let mut data = data.lock().unwrap();
下面分别解释一下是做什么的:
简单的说Arc::new表明了这是通过clone()方法来使用的,每clone,都会给该对象原子计数+1,通过引用计数的方法来保证对象只要还被其中任何一个线程引用就不会被释放掉,从而保证了前面说的:这个类型在多线程并发时没有导致内存不安全的可能性。
如果我们不定义为Arc<>就传到其他线程使用,编译器会报:
error: capture of moved value: `data`
data[i] += 1;
我们可以记住clone()就是Arc的用法。
接下来我们看Mutex:
Mutex实现了send接口。同样,在权威资料里是这么描述的:这个类型的所有权可以在线程间安全的转移
那我们又是怎么用Mutex的呢?就是用lock().unwrap()。lock()的作用是获取对象,如果当前有其他线程正在使用Mutex<T>里面的T对象时,本线程就会阻塞,从而保证同时只有一个线程来访问对象,mutex也另外提供了try_lock()的方法,是不阻塞的,只要其他线程被占用,就返回err,通常Arc和Mutex都是一起使用的。
回到我最原始的题目,Mutex和Arc实现了对象本身的线程共享,但是在线程间如何传递这个对象呢?就是靠channel,channel通常是这么定义的let (tx, rx) = mpsc::channel();它会返回两个对象tx和rx,就是之前我提到的sender和receiver。
在我的Rust实现里,关键的语句是以下几个:
let leaders = (0..leader_cnt).map(|i|
Arc::new(Mutex::new(Leader::new(i,dance_types.len() as i32)))
).collect::<Vec<_>>();
这一句是new一堆leader出来,Arc和Mutex表明leader是可以多线程共享和访问的。
同样Follower也是:
let followers = (0..follower_cnt).map(|i|
Arc::new(Mutex::new(Follower::new(i,dance_types.len() as i32,leader_cnt)))
).collect::<Vec<_>>();
接下来这几句就有点不好理解了。

这里定义了一堆的sender和receiver,其中把他们都作为leader和follower的成员变量存起来。大概意思就是每一个leader都通过sender列表可以发送invitation给所有follower,同时又有单个receiver来接受所有follower发给自己的处理结果inviresult。
同样follower也是这么做。这样在之后每一个follower和leader作为一个线程跑起来之后,都能在相互之间建立了一条通信的通道。
这个是和Java实现多线程并发最大的不同之处!Java是通过给对象加锁,Rust是通过channel转移对象的所有权,在代码里,leader发送inv给folloer是下面这一句
match self.senders[*follower_id as usize].lock().unwrap().send(inv){,其中的lock().unwrap()是获得该leader对该follower的发送通道的所有权,send(inv)就是转移具体的发送对象invitation所有权了。
这个转移按照我的理解,应该是内存拷贝。就是在follower接收的时候,let inv = match self.receiver.recv() { ,原来leader里面的inv在send之后已经是不可访问了,如果你之后再次访问了inv,会报use of moved value错误,而follower里面的inv则是在follower的栈里新生成的对象,所以,在Java里面我只定义了invitation对象,但是在Rust里面,我要再定义一个InviResult,因为我即使在follower线程里面填了result字段,leader线程也不能继续访问inv了。所以需要依靠follower再次发送一个invresult给leader,所以整个Rust程序大概就是这么一个思路。
实践总结
之前我测试比较Java和Rust实现的性能时,由于没有把调试信息去掉,导致Java比Rust慢很多,特别是那些调试信息都是调用String.format,这是比几个string相加慢上10倍的方法,两者都去掉调试信息后,leader和follower都会2000的时候,在我低端外星人笔记本里,性能差别大概是2倍吧,没我想象中大,Rust的程序整个写下来比较费力,一方面是对ownership机制不熟,思维没有转变过来,另一方面Rust的确需要开发者分部分精力到语法细节上。
编者注:冯总也有一些其它的实践体会,请参见CSDN对冯耀明的专访,请戳这里。也可以查看他的个人博客里的总结。
下面摘录采访中关于Rust的内容过来:
首先Rust里面的ownership和lifetime概念真的很酷,就因为这个概念实现无内存泄露,野指针和安全并发。
其次,Rust的语法不简单,也是有不少坑的,据说Rust的潜在用户应该是现在的C和C++程序员,他们可能会觉得比较习惯,说不定还 觉得更简单。由于ownership机制,一些在其他语言能够跑通的程序在Rust下就要调整实现了,它会改变你写程序的思维方式。据说一些写Rust超 过半年的程序员已经爱上它了!
我对Rust感受较深的是下面几点:
初学者不熟悉ownership机制,会无数次编译失败。但一旦编译成功,那么程序只剩下逻辑错误了。同样,由于ownership机制,将来在项目里修改Rust代码将可能是痛苦的过程,因为原来编译通过的代码可能加入新功能就编译不过了,这是我的猜测。
Rust编译速度慢,不过据说最近每一个Rust新发布的版本编译速度都比之前的版本提高了30%。
Rust没有类,有的是结构体加方法,我喜欢这种简单的概念。
Rust没有类继承,只有接口,虽然接口可以提供默认的实现。这样一来,在大型项目里原来类继承来重用代码的效果是否就要用成员变量实例来完成呢?
Rust没有null,取而代之的是None和Option<T>,也因此,结构体在初始化的时候必须初始化所有字段。
Rust有我一直很想要的错误值返回机制,而不必通过抛异常或者需要每每定义包含结果和错误体实现。
Rust用send和sync两个接口来处理多线程并发,其中Arc<T>和Mutex<T>分别实现了这两个接口,简单易用。
Rust目前没有一个强大的IDE,支持断点调试,变量监控等。
它跟现在动态语言是两个截然不同的方向,它适合一些资深的程序员,我倒是觉得有必要有这么一本书,叫《从C++到Rust,你需要改善的20个编程 习惯》,能从实践上告诉开发者Rust里我们应该遵从什么样的编程习惯。Rust未来是否像C那样流行开来成为新一代的主流语言没有人能够知道,但它绝对 是值得你去了解和关注的语言。
进一步的思考:反转链表 - Java和Rust的不同实现
Rust的list应该怎么定义,譬如反转列表又是怎么做呢?
由于ownership的机制和不存在空指针的情况,很多在其他带GC的语言能够跑起来的程序在Rust下面就要换一种做法。最近试用Rust的基础数据结构时,更加加强了我的看法。下面以最原始的链表list为例。
在Java中,考虑最基本的链表定义
class ListNode {
int val;
ListNode next;

ListNode(int x) {
val = x;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(val);
ListNode pNext = this.next;
while (pNext != null) {
sb.append(",");
sb.append(pNext.val);
pNext = pNext.next;
}
sb.append("]");
return String.format("%s", sb.toString());
}
}
如果我们要反转链表,可以这么做:
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode pNext = head.next;
ListNode pPrevious = null;
while (head != null) {
pNext = head.next;
head.next = pPrevious;
pPrevious = head;
head = pNext;
}
return pPrevious;
}
那如果我们按照一般思维,在Rust里对应的实现就是这样子的:
struct ListNode{
id :i32,
next :Option<Box<ListNode>>
}
反转链表:
fn reverseList2(head :&mut Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match *head{
None => None,
Some(head) => {
let mut head = Some(head);
let mut pNext = head.unwrap().next;
let mut pPrevious:Option<Box<ListNode>> = None;
while true {
match head {
None =>{break;}
_ =>{}
}
pNext = head.unwrap().next;
head.unwrap().next = pPrevious;
pPrevious = head;
head = pNext;
}
pPrevious
}
}
}
然后编译,报了以下错误:
=》match *head{

ERROR:cannot move out of borrowed content
=》 pNext = head.unwrap().next;
ERROR:cuse of moved value: `head`

这些错误就是因为Rust的ownership机制,让我们无法像Java或者C++里保存临时变量,特别是在循环里。反复试过各种写法,都行不通。
最后,换成这么来做
链表定义:
use List::*;

enum List {
Cons1(i32, Box<List>),
Nil,
}

// Methods can be attached to an enum
impl List {
#[inline]
fn new() -> List {
Nil
}

#[inline]
fn prepend(self, elem: i32) -> List {
Cons1(elem, Box::new(self))
}

fn len(&self) -> i32 {
match *self {
Cons1(_, ref tail) => 1 + tail.len(),
Nil => 0
}
}

fn stringify(&self) -> String {
match *self {
Cons1(head, ref tail) => {
format!("{}, {}", head, tail.stringify())
},
Nil => {
format!("Nil")
},
}
}
}

fn reverseList(list:List, acc:List ) -> List{
match list{
Cons1(val,tail) => {
reverseList(*tail,acc.prepend(val))
}
Nil => acc
}
}

fn main() {
let mut head = List::new();
let mut i=0;
while i < 10 {
i+=1;
head = head.prepend(i);
}
println!("{:30}",head.stringify());
let result = List::new();
let result = reverseList(head,result);
<span style="white-space:pre"> </span>println!("{:30}",result.stringify());
}
从结果可以看到,链表已经实现反转了。所以在Rust下面,很多做法都要换一下。有人说这就是Rust函数式编程的思维。我但愿这种递归式的做法不会有溢出。

阅读全文

与rust加快编译速度相关的资料

热点内容
dvd光盘存储汉子算法 浏览:757
苹果邮件无法连接服务器地址 浏览:962
phpffmpeg转码 浏览:671
长沙好玩的解压项目 浏览:144
专属学情分析报告是什么app 浏览:564
php工程部署 浏览:833
android全屏透明 浏览:737
阿里云服务器已开通怎么办 浏览:803
光遇为什么登录时服务器已满 浏览:302
PDF分析 浏览:484
h3c光纤全工半全工设置命令 浏览:143
公司法pdf下载 浏览:381
linuxmarkdown 浏览:350
华为手机怎么多选文件夹 浏览:683
如何取消命令方块指令 浏览:349
风翼app为什么进不去了 浏览:778
im4java压缩图片 浏览:362
数据查询网站源码 浏览:150
伊克塞尔文档怎么进行加密 浏览:892
app转账是什么 浏览:163