Rust集成开发环境插件iRust的使用,iRust提供增强型REPL交互式编程和代码补全功能
IRust - 跨平台Rust REPL
安装
cargo install irust
主要功能
iRust是一个增强型的Rust REPL环境,提供代码补全、交互式编程等功能。
基本命令
// 显示帮助
:help
// 重置REPL环境
:reset
// 显示当前REPL代码
:show
// 添加依赖
:add serde
// 查看表达式类型
:type vec![5]
// 计时执行
:time 5+4
:time my_func(arg1, arg2)
// 加载Rust文件
:load path/to/file.rs
// 编辑缓冲区
:edit
示例代码
// 基本表达式计算
let x = 5 + 3;
println!("x = {}", x); // 输出: x = 8
// 使用向量
let v = vec![1, 2, 3];
v.iter().map(|x| x * 2).collect::<Vec<_>>(); // 返回: [2, 4, 6]
// 添加外部crate依赖
:add rand
use rand::Rng;
rand::thread_rng().gen_range(1..=100); // 生成1-100的随机数
// 查看类型
:type HashMap::<i32, String>::new(); // 输出: std::collections::HashMap<i32, String>
// 计时执行
:time {
let mut sum = 0;
for i in 1..=100000 {
sum += i;
}
sum
}
配置
iRust的配置文件位于:
- Linux:
/home/$USER/.config/irust/config.toml
- Windows:
C:\Users\$USER\AppData\Roaming/irust/config.toml
- Mac:
/Users/$USER/Library/Application Support/irust/config.toml
默认配置:
[history]
add_irust_cmd_to_history = true
add_shell_cmd_to_history = false
[colors]
ok_color = "Blue"
eval_color = "White"
irust_color = "DarkBlue"
[rust_analyzer]
enable_rust_analyzer = false
ra_max_suggestions = 5
[other]
toolchain = "stable"
check_statements = true
主题配置
[keyword]
function = "blue"
type = "cyan"
macro = "dark_yellow"
literal = "yellow"
comment = "dark_grey"
高级功能
// 使用异步
:executor tokio
async fn fetch_data() -> Result<String, reqwest::Error> {
reqwest::get("https://example.com").await?.text().await
}
// 调试
:dbg factorial(5)
// 宏展开
:expand println!("Hello")
// 查看汇编
:asm factorial
// 使用脚本
:scripts
键位绑定
- Ctrl+l: 清屏
- Ctrl+c: 清除当前行
- Ctrl+d: 退出(如果缓冲区为空)
- Tab/ShiftTab: 代码补全循环
- Ctrl+e: 强制评估当前表达式
完整示例代码
// 1. 启动iRust后,先添加需要的依赖
:add serde
:add rand
// 2. 使用添加的crate
use serde::{Serialize, Deserialize};
use rand::Rng;
// 3. 定义一个结构体并序列化
#[derive(Serialize, Deserialize, Debug)]
struct Point {
x: i32,
y: i32,
}
let point = Point { x: 10, y: 20 };
let serialized = serde_json::to_string(&point).unwrap(); // 序列化为JSON字符串
println!("{}", serialized); // 输出: {"x":10,"y":20}
// 4. 使用随机数
let mut rng = rand::thread_rng();
let random_num: u32 = rng.gen_range(1..=100);
println!("随机数: {}", random_num);
// 5. 查看类型
:type point // 输出: Point
// 6. 计时执行
:time {
let v: Vec<i32> = (0..10000).collect();
v.iter().sum::<i32>()
}
// 7. 调试代码
fn factorial(n: u32) -> u32 {
if n <= 1 { 1 } else { n * factorial(n-1) }
}
:dbg factorial(5) // 调试阶乘函数
// 8. 宏展开
:expand println!("Hello, {}", "world");
// 9. 从文件加载代码
:load /path/to/example.rs
1 回复
iRust - Rust增强型REPL交互式编程环境插件
简介
iRust是一个Rust集成开发环境插件,为Rust开发者提供了增强型的REPL(Read-Eval-Print Loop)交互式编程体验和强大的代码补全功能。它可以在你喜欢的IDE或编辑器中运行,让Rust代码的探索和实验变得更加高效。
主要特性
- 增强型REPL环境
- 智能代码补全
- 语法高亮
- 历史命令记录
- 多行编辑支持
- 快速文档查看
安装方法
通过cargo安装
cargo install irust
在VS Code中使用
- 打开VS Code扩展市场
- 搜索"iRust"
- 点击安装
基本使用方法
启动iRust
irust
启动后你会看到一个交互式提示符 >>
,可以开始输入Rust代码。
基本示例
>> let x = 5;
>> let y = 10;
>> x + y
15
多行输入
使用 :paste
命令进入多行编辑模式:
>> :paste
Entering paste mode (ctrl-D to finish)
fn add(a: i32, b: i32) -> i32 {
a + b
}
>> add(3, 4)
7
查看帮助
>> :help
常用命令
:clear
- 清屏:reset
- 重置REPL环境:vars
- 显示当前定义的变量:funcs
- 显示当前定义的函数:type <expr>
- 显示表达式类型:doc <item>
- 查看文档
高级功能
代码补全
在输入时按Tab键可以触发代码补全:
>> std::collections::Hash<TAB>
HashMap HashSet
历史记录
使用上下箭头键浏览历史命令。
加载外部crate
>> :dep rand = "0.8"
>> rand::random::<f64>()
0.5488135039273248
配置选项
可以通过 ~/.irust/irust.toml
文件进行配置:
[theme]
prompt = ">> "
success_color = "green"
error_color = "red"
[editor]
use_external_editor = true
editor = "code -w"
与标准Rust REPL的区别
iRust相比标准Rust REPL提供了:
- 更丰富的交互功能
- 更好的代码补全
- 更直观的错误提示
- 更强大的多行编辑支持
- 直接的外部crate加载能力
示例项目探索
>> :dep serde_json = "1.0"
>> let data = r#"{"name": "John", "age": 30}"#;
>> let parsed: serde_json::Value = serde_json::from_str(data).unwrap();
>> parsed["name"]
"John"
完整示例demo
// 启动iRust
$ irust
// 定义变量和函数
>> let name = "Alice";
>> let age = 25;
>> fn greet(name: &str, age: i32) { println!("Hello, {}! You are {} years old.", name, age); }
// 调用函数
>> greet(name, age)
Hello, Alice! You are 25 years old.
// 使用外部crate
>> :dep chrono = "0.4"
>> use chrono::Local;
>> Local::now()
2023-11-15T14:30:22.123456789+08:00
// 多行函数定义
>> :paste
Entering paste mode (ctrl-D to finish)
fn factorial(n: u32) -> u32 {
match n {
0 => 1,
_ => n * factorial(n - 1)
}
}
>> factorial(5)
120
// 查看类型
>> :type factorial
fn(u32) -> u32
// 查看文档
>> :doc chrono::Local
[显示chrono::Local的文档]
// 清屏
>> :clear
// 显示当前变量
>> :vars
name = "Alice"
age = 25
iRust是Rust开发者快速测试代码片段、学习语言特性和探索库功能的强大工具,可以显著提高开发效率。