Created
December 25, 2020 18:51
-
-
Save sangam14/4f6fa5ab54b0ff213dce110db2ac594d to your computer and use it in GitHub Desktop.
rustlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rustup.sh # install rust, see rust-lang.org for details | |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh | |
$ cargo new bin # start new executable project | |
$ ls -lR # list our skeleton of files | |
src/main.rs # main.rs, has main() entry point | |
Cargo.toml # Cargo.toml defines packaging | |
$ $EDITOR Cargo.toml # add dependencies and other details | |
[package] | |
name = "helloworld" | |
version = "0.1.0" | |
authors = ["rustlabs <[email protected]>"] | |
[dependencies] | |
serde = "1.0.80" | |
$ cargo build # downloads dependencies + builds main.rs | |
$ cargo run # runs program created from main.rs | |
$ cargo test # runs tests (in parallel by default) | |
$ cargo test -- --test-threads=1 # run tests one at a time | |
$ cargo test -- --nocapture # run tests, show output | |
world famous hello world | |
``` | |
fn main() { | |
println!("Hello, world!"); | |
} | |
``` | |
variable and placeholder | |
``` | |
fn main() { | |
let name ="sangam" ; | |
println!("Hello, {} ",name); | |
} | |
``` | |
refine varible 2 times | |
``` | |
fn main() { | |
let name = "sangam"; | |
println!("Hello, {}", name); | |
name = "biradar"; | |
println!("Hello, {}", name); | |
} | |
``` | |
``` | |
fn main() { | |
let mut name = "Sangam"; | |
println!("Hello, {}", name); | |
name = "Rust"; | |
println!("Hello, {}", name); | |
} | |
``` | |
``` | |
fn main() { | |
println!("{}, {}!", "Hello", "world"); // Hello, world! | |
println!("{0}, {1}!", "Hello", "world"); // Hello, world! | |
println!("{greeting}, {name}!", greeting="Hello", name="world"); // Hello, world! | |
println!("{:?}", [1,2,3]); // [1, 2, 3] | |
println!("{:#?}", [1,2,3]); | |
/* | |
[ | |
1, | |
2, | |
3 | |
] | |
*/ | |
// 🔎 The format! macro is used to store the formatted string. | |
let x = format!("{}, {}!", "Hello", "world"); | |
println!("{}", x); // Hello, world! | |
// 💡 Rust has a print!() macro as well | |
print!("Hello, world!"); // Without new line | |
println!(); // A new line | |
print!("Hello, world!\n"); // With new line | |
} | |
```` | |
``` | |
fn add(a: i32, b: i32) -> i32 { | |
a + b | |
} | |
fn main() { | |
println!("1 + 1 = {}", add(1, 1)); | |
println!("13 + 23 = {}", add(13, 23)); | |
} | |
``` | |
``` | |
fn add(a: i32, b: i32) -> i32 { | |
let answer: i32; | |
answer = a + b; | |
answer | |
} | |
fn main() { | |
println!("1 + 1 = {}", add(1, 1)); | |
println!("13 + 23 = {}", add(13, 23)); | |
} | |
``` | |
``` | |
fn add(a: i32, b: i32) -> i32 { | |
a + b | |
} | |
fn sub(a: i32, b: i32) -> i32 { | |
return a - b; | |
} | |
fn main() { | |
println!("1 + 1 = {}", add(1, 1)); | |
println!("13 + 23 = {}", add(13, 23)); | |
println!("23 - 13 = {}", sub(23, 13)); | |
} | |
array | |
``` | |
let a = [1, 2, 3]; // a: [i32; 3] | |
let mut m = [1, 2, 3]; // m: [i32; 3] | |
fn main() { | |
let a = [1, 2, 3]; | |
println!("0th element {}", a[0]); | |
println!("1th element : {}", a[1]); | |
println!("2th element: {}", a[2]); | |
} | |
``` | |
length of array | |
``` | |
fn main() { | |
let a = [1, 2, 3,4]; | |
println!("# of items in a: {}", a.len()); | |
} | |
``` | |
``` | |
fn main(){ | |
let x = 5; | |
if x > 10 { | |
println!("x > 10") | |
}else if x > 0{ | |
println!("x < x <= 10") | |
}else{ | |
println!("x < 0") | |
}; | |
} | |
``` | |
if is a expression, not statement in Rust So, it has an evaluation value | |
``` | |
fn main(){ | |
let x = 5; | |
let y = if x > 0{ | |
1 | |
}else{ | |
0 | |
}; | |
println!("y = {}", y); | |
} | |
``` | |
loop statement | |
``` | |
fn main() { | |
let mut i = 0; | |
loop{ | |
i = i +1; | |
println!("{} sangam biradar ", i); | |
if i > 9{ | |
break; | |
} | |
} | |
} | |
``` | |
for loop range | |
``` | |
fn main() { | |
for x in 0..10 { | |
println!("{}", x); // x: i32 | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment