Code Block Style Test
A rust code block
fn fibonacci(n: u32) -> u32{
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}fn main(){
let result = fibonacci(10);
println!("The 10th Fibonacci number is: {}", result);
}
Inlinecode
Elements
When working with Rust variables likelet x = 42;
or functions likefibonacci()
, it's important to remember thatu32
andi32
are different types.
Keyboard kbd Elements
PressCtrl + Shift + Pto open the command palette.
Sample OutputsampElements
Terminal output:cargo build --release
Mixed Inline Elements
TheOption<T>
type in Rust can beSome(T)
orNone
. When you run the program you might seeHello, world!as output.
Complex Code Block
use std::collections::HashMap;
struct Cache{
data: HashMap<String, String>,
}impl Cache{
fn new() -> Self {
Cache {
data: HashMap::new(),
}
}
fn insert(&mut self, key: &str, value: &str) -> Option<String> {
self.data.insert(key.to_string(), value.to_string())
}
fn get(&self, key: &str) -> Option<&String> {
self.data.get(key)
}
}fn main(){
let mut cache = Cache::new();
cache.insert("name", "Rust");
if let Some(value) = cache.get("name") {
println!("Found: {}", value);
}
}