├── Hello.rs ├── program ├── if.rs ├── boolean.rs ├── variable.rs ├── if_else.rs ├── forloop.rs ├── continue.rs ├── comment.rs ├── whileloop.rs ├── loop.rs ├── elseif.rs ├── constants.rs ├── integers.rs ├── function_with_return.rs ├── mutable.rs ├── function_without_return.rs ├── number_separator.rs ├── integerrange.rs └── print_primitives.rs └── README.md /Hello.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /program/if.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | let num:i32=5; 3 | if num>0{ 4 | println!("number is positive"); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /program/boolean.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | let isfun:bool=true; 3 | println!("Is Rust Programming fun? {}",isfun); 4 | } 5 | -------------------------------------------------------------------------------- /program/variable.rs: -------------------------------------------------------------------------------- 1 | // Declaring a variable 2 | fn main() { 3 | let answer = 42; 4 | println!("Hello {}", answer); 5 | } 6 | -------------------------------------------------------------------------------- /program/if_else.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | let num:i32=12; 3 | if num%2==0{ 4 | println!("even"); 5 | }else{ 6 | println!("odd"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /program/forloop.rs: -------------------------------------------------------------------------------- 1 | // Program to print numbers from 1 to 10 using For loop. 2 | fn main() { 3 | 4 | for n in 1..11 { 5 | println!("{}", n); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /program/continue.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | 3 | for x in 1..11 { 4 | if x==5{ 5 | continue; 6 | } 7 | println!("{}", x); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /program/comment.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // single line comment 3 | let n = 100; // Write the comment here 4 | /* 5 | Multi-line comment 6 | */ 7 | println!("number is {}",n) 8 | } 9 | -------------------------------------------------------------------------------- /program/whileloop.rs: -------------------------------------------------------------------------------- 1 | // A Rust program to print numbers from 10 to 1. 2 | fn main() { 3 | 4 | let mut n = 10; 5 | 6 | while n!=0 { 7 | println!("{}", n); 8 | n-=1; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /program/loop.rs: -------------------------------------------------------------------------------- 1 | //Program to print multiplication table of 2 2 | fn main() { 3 | let mut n = 1; 4 | 5 | loop { 6 | println!("{}", n*2); 7 | n+=1; 8 | if n>10{ 9 | break; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /program/elseif.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | let num=3; 3 | if num>0{ 4 | println!("{} is positive",num); 5 | }else if num<0{ 6 | println!("{} is negative",num); 7 | }else{ 8 | println!("{} is neither negative nor positive",num); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /program/constants.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | // Constants represents values that cannot be changed. If you declare 3 | // a constant then there is no way its value changes. 4 | const PI:f32=3.14; // declare float constant 5 | const NUM: i32=100; // declare integer constant 6 | println!("{},{}",PI,NUM); 7 | } 8 | -------------------------------------------------------------------------------- /program/integers.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let result = 10; //i32 by default 3 | let age:u32=20; 4 | let sum:i32=5-15; 5 | println!("result is {}",result); 6 | println!("sum is {} and age is {}", sum,age); 7 | 8 | let small_number1 = 10u8; 9 | println!("small number is {}",small_number1); 10 | } 11 | -------------------------------------------------------------------------------- /program/function_with_return.rs: -------------------------------------------------------------------------------- 1 | fn multiply(number_one: i32, number_two: i32) -> i32 { 2 | let result = number_one * number_two; 3 | result // this is the i32 that we return 4 | } 5 | 6 | fn main() { 7 | let multiply_result = multiply(8, 9); 8 | println!("8 times 9 is {} ", multiply_result); 9 | } 10 | -------------------------------------------------------------------------------- /program/mutable.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | // Variables are immutable by default, i.e. the variable's value cannot be changed 3 | // once a value is bound to a var_name. TO make it mutable, we have to prefix the 4 | // variable name with (mut) keyword. Then the value can be chanegd. 5 | 6 | let mut fees=25_000; 7 | println!("fees is {}",fees); 8 | fees=35_000; 9 | println!("fees changed to {}",fees); 10 | } 11 | -------------------------------------------------------------------------------- /program/function_without_return.rs: -------------------------------------------------------------------------------- 1 | fn multiply(number_one: i32, number_two: i32) { // Two i32s will enter the function. We will call them number_one and number_two. 2 | let result = number_one * number_two; 3 | println!("{} times {} is {}", number_one, number_two, result); 4 | } 5 | 6 | fn main() { 7 | multiply(8, 9); // We can give the numbers directly 8 | let some_number = 10; // Or we can declare two variables 9 | let some_other_number = 2; 10 | multiply(some_number, some_other_number); // and put them in the function 11 | } 12 | -------------------------------------------------------------------------------- /program/number_separator.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | // For easy readability of large numbers, we can use a visual separator(_) underscore 3 | // to separate the digits, it will have no effect on the code or variable. 4 | let small_number = 10_u8; // This is easier to read 5 | let big_number = 100_000_000_i32; // 100 million is easy to read with _ 6 | println!("{}, {}", small_number, big_number); 7 | 8 | let number1 = 0________u8; 9 | let number2 = 1___6______2____4______i32; 10 | println!("{}, {}", number1, number2); 11 | } 12 | -------------------------------------------------------------------------------- /program/integerrange.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | println!("The smallest i8 is {} and the biggest i8 is {}.", i8::MIN, i8::MAX); 3 | println!("The smallest u8 is {} and the biggest u8 is {}.", u8::MIN, u8::MAX); 4 | println!("The smallest i16 is {} and the biggest i16 is {}.", i16::MIN, i16::MAX); 5 | println!("The smallest u16 is {} and the biggest u16 is {}.", u16::MIN, u16::MAX); 6 | println!("The smallest i32 is {} and the biggest i32 is {}.", i32::MIN, i32::MAX); 7 | println!("The smallest u32 is {} and the biggest u32 is {}.", u32::MIN, u32::MAX); 8 | println!("The smallest i64 is {} and the biggest i64 is {}.", i64::MIN, i64::MAX); 9 | println!("The smallest u64 is {} and the biggest u64 is {}.", u64::MIN, u64::MAX); 10 | println!("The smallest i128 is {} and the biggest i128 is {}.", i128::MIN, i128::MAX); 11 | println!("The smallest u128 is {} and the biggest u128 is {}.", u128::MIN, u128::MAX); 12 | } 13 | -------------------------------------------------------------------------------- /program/print_primitives.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // {} will be automatically replaced with any arguments. 3 | println!("{} days", 31); 4 | 5 | // Specifying an integer inside `{}` 6 | // determines which additional argument will be replaced. Arguments start 7 | // at 0 immediately after the format string. 8 | println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob"); 9 | 10 | // Different formatting can be invoked by specifying the format character 11 | // after a `:`. 12 | println!("Base 10: {}", 69420); // 69420 13 | println!("Base 2 (binary): {:b}", 69420); // 10000111100101100 14 | println!("Base 8 (octal): {:o}", 69420); // 207454 15 | println!("Base 16 (hexadecimal): {:x}", 69420); // 10f2c 16 | 17 | // You can right-justify text with a specified width. This will 18 | // output " 1". (Four white spaces and a "1", for a total width of 5.) 19 | println!("{number:>5}", number=1); 20 | 21 | // You can pad numbers with extra zeroes, 22 | println!("{number:0>5}", number=1); // 00001 23 | // and left-adjust by flipping the sign. This will output "10000". 24 | println!("{number:0<5}", number=1); // 10000 25 | 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
Rust For All