fn main() {
// Type declared with var: <T> syntax
let penguin_one: &str = "gentoo";
// Type &str is inherited from "gentoo"
let penguin_two = "gentoo";
// Will not panic if they are the same
assert_eq!(penguin_one, penguin_two);
}
Wrong Way:
fn print_u8_vector(vec: Vec<u8>) {
println!("{:?}", vec);
}
fn main() {
let penguin_ages: Vec<u8> = vec!(2, 4, 6);
print_u8_vector(penguin_ages);
// This line will throw an error
println!("{}", penguin_ages[0]);
}
Correct Way:
fn print_u8_vector(vec: &Vec<u8>) {
println!("{:?}", vec);
}
fn main() {
let penguin_ages: Vec<u8> = vec!(2, 4, 6);
print_u8_vector(&penguin_ages);
// This line will print '2'
println!("{}", penguin_ages[0]);
}
Wrong Way:
fn main() {
let my_num = 2;
// This line will throw an error
my_num = my_num + 1;
println!("{}", my_num);
}
Correct Way:
fn main() {
let mut my_num = 2;
my_num = my_num + 1;
// This line will print '3'
println!("{}", my_num);
}
Hello World Program in C in Rust:
extern "C" {
fn printf(input: &str);
}
fn main() {
unsafe {
printf("Hello, World!");
}
}
Email: izzyleibowitz at pm dot me
Mastodon: at blackernel at nixnet dot social
Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.
The HPR Website Design is released to the Public Domain.