Adds rust demo.

This commit is contained in:
Henning Dieterichs
2020-03-22 17:04:02 +01:00
parent 75062b67cc
commit 78c8e6c875
5 changed files with 127 additions and 0 deletions

10
demos/rust/.gitignore vendored Normal file
View File

@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
/target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk

16
demos/rust/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/target/debug/rust_demo",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

11
demos/rust/Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "rust_demo"
version = "0.1.0"
authors = ["hediet"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

89
demos/rust/src/main.rs Normal file
View File

@@ -0,0 +1,89 @@
use serde::Serialize;
#[derive(Debug, Serialize)]
struct Label {
label: Option<String>,
}
#[derive(Debug, Serialize)]
struct Row {
label: Option<String>,
columns: Vec<Column>,
}
#[derive(Debug, Serialize)]
struct Column {
content: Option<String>,
tag: Option<String>,
color: Option<String>,
}
#[derive(Debug, Serialize)]
struct Marker {
id: String,
row: u64,
column: u64,
rows: Option<u64>,
columns: Option<u64>,
label: Option<String>,
color: Option<String>,
}
#[allow(clippy::trivially_copy_pass_by_ref)]
fn is_false(b: &bool) -> bool {
!*b
}
#[derive(Debug, Serialize)]
struct Kind {
#[serde(skip_serializing_if = "is_false")]
array: bool,
}
#[derive(Debug, Serialize)]
pub struct Grid {
kind: Kind,
#[serde(rename = "columnLabels")]
column_labels: Option<Vec<Label>>,
rows: Vec<Row>,
markers: Option<Vec<Marker>>,
}
fn show_arr(a: &[i32]) -> String {
let n = a.len();
let labels: Vec<Label> = (0..n)
.map(|i| Label {
label: Some(i.to_string()),
})
.collect();
let columns: Vec<Column> = a
.iter()
.map(|x| Column {
content: Some(format!("{:?}", x)),
tag: Some(x.to_string()),
color: None,
})
.collect();
let row = Row {
label: None,
columns,
};
let kind = Kind { array: true };
let grid = Grid {
kind,
column_labels: Some(labels),
rows: vec![row],
markers: None,
};
serde_json::to_string(&grid).unwrap()
}
fn main() {
let mut arr = vec![1, 2, 3];
let mut _s = show_arr(&arr);
for _ in 0..5 {
arr.swap(0, 2); // break point
_s = show_arr(&arr);
}
dbg!(arr); // break point
println!("Hello, world!");
}

View File

@@ -18,6 +18,7 @@ See [demos](./demos/) for demos. These languages and debuggers are verified to w
- Java using `java` debug adapter: [✅ Rudimentary Support](../demos/java)
- C++ using `cppdbg` debug adapter: [✅ Rudimentary Support](../demos/cpp)
- Swift using `lldb` debug adapter: [✅ Rudimentary Support](../demos/swift)
- Rust using `lldb` debug adapter: [✅ Rudimentary Support](../demos/rust)
All other languages and debuggers might work too.
For languages with _Rudimentary Support_, only JSON strings can be visualized - you must implement some logic that builds this JSON for your data structure!