diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..5fb43ef --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,2 @@ +indent_style = "Block" +reorder_imports = false diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3f139ea --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "rust-for-undergrads" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + diff --git a/README.md b/README.md index 91d134f..5f8c4d3 100644 --- a/README.md +++ b/README.md @@ -9,16 +9,17 @@ Goals: To make it easy for undergrads to start programming in Rust. To advocate Level: Beginner/Intermediate Resources: + * [A Guide to Porting C/C++ to Rust](https://www.gitbook.com/book/locka99/a-guide-to-porting-c-to-rust/details) * [Rust by Example](https://rustbyexample.com/) * [Rustlings](https://github.com/carols10cents/rustlings) * [Rosetta Code](http://rosettacode.org/wiki/Category:Rust) * [exercism.io](http://exercism.io/languages/rust/about) - ![Poster](https://github.com/rustindia/Rust-for-undergrads/blob/master/Rust_for_undergrads.png) Community: + 1. [rust-beginners](https://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust-beginners), an IRC channel that loves answering questions at any depth. 2. [Rust Subreddit](https://www.reddit.com/r/rust), a place where Rust geeks love to engage. 3. [The Rust Users Forum](https://users.rust-lang.org/), for discussion of all things Rust. diff --git a/lint.sh b/lint.sh new file mode 100644 index 0000000..2d3cd74 --- /dev/null +++ b/lint.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# +# Usage example: +# bash lint.sh +# + +echo "==> Formatting project files..." +cargo fmt --all +rustfmt src/*.rs diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..41fb154 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,3 @@ +[toolchain] +channel = "nightly-2022-09-18" +components = ["rustfmt", "clippy"] \ No newline at end of file diff --git a/setup.sh b/setup.sh new file mode 100644 index 0000000..9345083 --- /dev/null +++ b/setup.sh @@ -0,0 +1,93 @@ +#!/bin/bash +# +# Usage example: +# bash setup.sh -b +# + +RUST_TOOLCHAIN="$(awk -F'[ ="]+' '$1 == "channel" { print $2 }' rust-toolchain.toml)" +INSTALL_BUILD_TOOLS="false" + +function start_message() { + cat < Installing Rust......" + if rustup --version &>/dev/null; then + echo "Rust is already installed" + rustc --version + else + curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain "${RUST_TOOLCHAIN}" --profile minimal + PATH="${HOME}/.cargo/bin:${PATH}" + source $HOME/.cargo/env + fi +} + +function install_default_toolchain { + RUST_TOOLCHAIN=$1 + + echo "==> Setting nightly toolchain..." + rustup install "${RUST_TOOLCHAIN}" + rustup override set "${RUST_TOOLCHAIN}" + + rustup component add rustfmt --toolchain "${RUST_TOOLCHAIN}" + rustup component add clippy --toolchain "${RUST_TOOLCHAIN}" +} + +start_message + +printf "Proceed with installing necessary dependencies? (y/N) > " +read -e -r input +if [[ "$input" != "y"* ]]; then + echo "Exiting..." + exit 0 +fi + +# Loop through command line arguments +while getopts "bc" arg; do + case "$arg" in + b) + INSTALL_BUILD_TOOLS="true" + ;; + *) + usage exit 0 + ;; + esac +done + +if [[ "$INSTALL_BUILD_TOOLS" == "true" ]]; then + install_rustup "$RUST_TOOLCHAIN" + install_default_toolchain "$RUST_TOOLCHAIN" + + # Any call to cargo will make rustup install the correct toolchain + cargo version +fi + +echo "==> Rust configured!" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..bb76b1a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Rust for Undergrads!"); +} diff --git a/src/tictactoe.rs b/src/tictactoe.rs index 06d3218..6a40844 100644 --- a/src/tictactoe.rs +++ b/src/tictactoe.rs @@ -1,6 +1,6 @@ //TIC TAK TOE /* - Authors- officialnizam,sangeethayashasvi + Authors- officialnizam,sangeethayashasvi */ //Inputs given according to numpad format //9 is row 1 colom 1 @@ -14,148 +14,222 @@ //1 is row 3 colom 3 use std::*; fn main() { - - let mut s =String::new(); - println!("Press Enter to Start The Game"); - io::stdin().read_line(& mut s).expect(""); - let mut arr = [[0u8; 4]; 6]; //all initialized to 7 - arr[0_usize][0_usize] = 7; - arr[0_usize][1_usize] = 7; - arr[0_usize][2_usize] = 7; - arr[1_usize][0_usize] = 7; - arr[1_usize][1_usize] = 7; - arr[1_usize][2_usize] = 7; - arr[2_usize][0_usize] = 7; - arr[2_usize][1_usize] = 7; - arr[2_usize][2_usize] = 7; - let mut x=true; - let mut i:u32; - let mut cntr=0; - let mut inv=false; - let mut ow=false; - let mut xw=false; - let mut dr=false; - loop - { - println!("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); //clear console - if inv - { - println!("Input was Invalid Play Again\n"); - inv=false; - } - if x{println!("O Turn");} - else{println!("X Turn");} - println!("_____________"); //custom formatting - for mut i in 0..3 - { - print!("| "); - for mut j in 0..3 - { - if arr[i][j]==1{print!("{} ",'X');} - else if arr[i][j]==0{print!("{} ",'O');} - else{print!("{} ",' ');} - print!("| "); - - } - - - println!(""); - println!("|___|___|___|"); //custom formatting - - } - println!(); - let mut str =String::new(); - io::stdin().read_line(& mut str).expect(""); - cntr+=1; - i=str.trim().parse().unwrap(); - let mut a=0; - let mut b=0; - if i==7{a=0;b=0;} - if i==8{a=0;b=1;} - if i==9{a=0;b=2;} - if i==4{a=1;b=0;} - if i==5{a=1;b=1;} - if i==6{a=1;b=2;} - if i==1{a=2;b=0;} - if i==2{a=2;b=1;} - if i==3{a=2;b=2;} - if x - { - if arr[a][b]!=7 - { - - inv=true; - x=true; - cntr-=1; - continue; - } - else - { - arr[a][b]=0; - x=false; - } - } - else - { - if arr[a][b]!=7 - { - inv=true; - x=false; - cntr-=1; - continue; - } - else - { - arr[a][b]=1; - x=true; - } - } - //first row - if arr[0][0]==0&&arr[0][1]==0&&arr[0][2]==0{ow=true;break;} - if arr[0][0]==1&&arr[0][1]==1&&arr[0][2]==1{xw=true;break;} - //second row - if arr[1][0]==0&&arr[1][1]==0&&arr[1][2]==0{ow=true;break;} - if arr[1][0]==1&&arr[1][1]==1&&arr[1][2]==1{xw=true;break;} - //third row - if arr[2][0]==0&&arr[2][1]==0&&arr[2][2]==0{ow=true;break;} - if arr[2][0]==1&&arr[2][1]==1&&arr[2][2]==1{xw=true;break;} - //first colom - if arr[0][0]==0&&arr[1][0]==0&&arr[2][0]==0{ow=true;break;} - if arr[0][0]==1&&arr[1][0]==1&&arr[2][0]==1{xw=true;break;} - //last colom - if arr[0][2]==0&&arr[1][2]==0&&arr[2][2]==0{ow=true;break;} - if arr[0][2]==1&&arr[1][2]==1&&arr[2][2]==1{xw=true;break;} - //middle colom - if arr[0][1]==0&&arr[1][1]==0&&arr[2][1]==0{ow=true;break;} - if arr[0][1]==1&&arr[1][1]==1&&arr[2][1]==1{xw=true;break;} - //first diagonal - if arr[0][0]==0&&arr[1][1]==0&&arr[2][2]==0{ow=true;break;} - if arr[0][0]==1&&arr[1][1]==1&&arr[2][2]==1{xw=true;break;} - //second diagonal - if arr[0][2]==0&&arr[1][1]==0&&arr[2][0]==0{ow=true;break;} - if arr[0][2]==1&&arr[1][1]==1&&arr[2][0]==1{xw=true;break;} - println!(""); - if cntr==9{dr=true;break;} - } - println!("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); - println!("Your Last Play-"); - println!("_____________"); - for mut i in 0..3 - { - print!("| "); - for mut j in 0..3 - { - if arr[i][j]==1{print!("{} ",'X');} - else if arr[i][j]==0{print!("{} ",'O');} - else{print!("{} ",' ');} - print!("| "); - } - - println!(""); - println!("|___|___|___|"); - } - if ow{println!("\nO wins");} - if xw{println!("\nX wins");} - if dr{println!("\nDraw!");} - + let mut s = String::new(); + println!("Press Enter to Start The Game"); + io::stdin().read_line(&mut s).expect(""); + let mut arr = [[0u8; 4]; 6]; //all initialized to 7 + arr[0_usize][0_usize] = 7; + arr[0_usize][1_usize] = 7; + arr[0_usize][2_usize] = 7; + arr[1_usize][0_usize] = 7; + arr[1_usize][1_usize] = 7; + arr[1_usize][2_usize] = 7; + arr[2_usize][0_usize] = 7; + arr[2_usize][1_usize] = 7; + arr[2_usize][2_usize] = 7; + let mut x = true; + let mut i: u32; + let mut cntr = 0; + let mut inv = false; + let mut ow = false; + let mut xw = false; + let mut dr = false; + loop { + println!("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); //clear console + if inv { + println!("Input was Invalid Play Again\n"); + inv = false; + } + if x { + println!("O Turn"); + } else { + println!("X Turn"); + } + println!("_____________"); //custom formatting + for mut i in 0..3 { + print!("| "); + for mut j in 0..3 { + if arr[i][j] == 1 { + print!("{} ", 'X'); + } else if arr[i][j] == 0 { + print!("{} ", 'O'); + } else { + print!("{} ", ' '); + } + print!("| "); + } + + println!(""); + println!("|___|___|___|"); //custom formatting + } + println!(); + let mut str = String::new(); + io::stdin().read_line(&mut str).expect(""); + cntr += 1; + i = str.trim().parse().unwrap(); + let mut a = 0; + let mut b = 0; + if i == 7 { + a = 0; + b = 0; + } + if i == 8 { + a = 0; + b = 1; + } + if i == 9 { + a = 0; + b = 2; + } + if i == 4 { + a = 1; + b = 0; + } + if i == 5 { + a = 1; + b = 1; + } + if i == 6 { + a = 1; + b = 2; + } + if i == 1 { + a = 2; + b = 0; + } + if i == 2 { + a = 2; + b = 1; + } + if i == 3 { + a = 2; + b = 2; + } + if x { + if arr[a][b] != 7 { + inv = true; + x = true; + cntr -= 1; + continue; + } else { + arr[a][b] = 0; + x = false; + } + } else { + if arr[a][b] != 7 { + inv = true; + x = false; + cntr -= 1; + continue; + } else { + arr[a][b] = 1; + x = true; + } + } + //first row + if arr[0][0] == 0 && arr[0][1] == 0 && arr[0][2] == 0 { + ow = true; + break; + } + if arr[0][0] == 1 && arr[0][1] == 1 && arr[0][2] == 1 { + xw = true; + break; + } + //second row + if arr[1][0] == 0 && arr[1][1] == 0 && arr[1][2] == 0 { + ow = true; + break; + } + if arr[1][0] == 1 && arr[1][1] == 1 && arr[1][2] == 1 { + xw = true; + break; + } + //third row + if arr[2][0] == 0 && arr[2][1] == 0 && arr[2][2] == 0 { + ow = true; + break; + } + if arr[2][0] == 1 && arr[2][1] == 1 && arr[2][2] == 1 { + xw = true; + break; + } + //first colom + if arr[0][0] == 0 && arr[1][0] == 0 && arr[2][0] == 0 { + ow = true; + break; + } + if arr[0][0] == 1 && arr[1][0] == 1 && arr[2][0] == 1 { + xw = true; + break; + } + //last colom + if arr[0][2] == 0 && arr[1][2] == 0 && arr[2][2] == 0 { + ow = true; + break; + } + if arr[0][2] == 1 && arr[1][2] == 1 && arr[2][2] == 1 { + xw = true; + break; + } + //middle colom + if arr[0][1] == 0 && arr[1][1] == 0 && arr[2][1] == 0 { + ow = true; + break; + } + if arr[0][1] == 1 && arr[1][1] == 1 && arr[2][1] == 1 { + xw = true; + break; + } + //first diagonal + if arr[0][0] == 0 && arr[1][1] == 0 && arr[2][2] == 0 { + ow = true; + break; + } + if arr[0][0] == 1 && arr[1][1] == 1 && arr[2][2] == 1 { + xw = true; + break; + } + //second diagonal + if arr[0][2] == 0 && arr[1][1] == 0 && arr[2][0] == 0 { + ow = true; + break; + } + if arr[0][2] == 1 && arr[1][1] == 1 && arr[2][0] == 1 { + xw = true; + break; + } + println!(""); + if cntr == 9 { + dr = true; + break; + } + } + println!("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); + println!("Your Last Play-"); + println!("_____________"); + for mut i in 0..3 { + print!("| "); + for mut j in 0..3 { + if arr[i][j] == 1 { + print!("{} ", 'X'); + } else if arr[i][j] == 0 { + print!("{} ", 'O'); + } else { + print!("{} ", ' '); + } + print!("| "); + } + + println!(""); + println!("|___|___|___|"); + } + if ow { + println!("\nO wins"); + } + if xw { + println!("\nX wins"); + } + if dr { + println!("\nDraw!"); + } } diff --git a/src/to_be_organized/finger.rs b/src/to_be_organized/finger.rs index 5bb73f5..29268f2 100644 --- a/src/to_be_organized/finger.rs +++ b/src/to_be_organized/finger.rs @@ -3,27 +3,23 @@ use std::io; /* TODO: Figure out what this program does. */ -fn read() -> i32{ +fn read() -> i32 { let mut num = String::new(); - io::stdin() - .read_line(&mut num) - .expect("error"); - num.trim().parse::() - .unwrap() + io::stdin().read_line(&mut num).expect("error"); + num.trim().parse::().unwrap() } -fn main(){ - +fn main() { println!("Enter number"); let a = read(); - let b = a%8; - if b == 1{ - println!("{}",b); + let b = a % 8; + if b == 1 { + println!("{}", b); } if b == 5 { - println!("{}",b); + println!("{}", b); } if b == 0 || b == 2 { println!("2"); @@ -34,5 +30,4 @@ fn main(){ if b == 4 || b == 6 { println!("4"); } - } diff --git a/src/to_be_organized/towers_of_hanoi.rs b/src/to_be_organized/towers_of_hanoi.rs index fc9076b..5864475 100644 --- a/src/to_be_organized/towers_of_hanoi.rs +++ b/src/to_be_organized/towers_of_hanoi.rs @@ -5,7 +5,7 @@ fn move_(n: i32, from: i32, to: i32, via: i32) { move_(n - 1, via, to, from); } } - + fn main() { move_(4, 1, 2, 3); } diff --git a/src/trivial_demonstrations/chars_example.rs b/src/trivial_demonstrations/chars_example.rs index 7269a55..caf068d 100644 --- a/src/trivial_demonstrations/chars_example.rs +++ b/src/trivial_demonstrations/chars_example.rs @@ -1,21 +1,21 @@ -use std::{i8,i16,u8}; +use std::{i8, i16, u8}; use std::io::stdin; -fn main(){ - let mut random="Asif Khan"; - let mut count=random.chars().count(); - println!("{}",count); +fn main() { + let mut random = "Asif Khan"; + let mut count = random.chars().count(); + println!("{}", count); - let mut characters=random.chars(); - let mut ind_char=characters.next(); + let mut characters = random.chars(); + let mut ind_char = characters.next(); - while(count<=random.chars().count()){ + while (count <= random.chars().count()) { match ind_char { - Some(x) => println!("{}",x), + Some(x) => println!("{}", x), None => break, } - ind_char=characters.next(); - count-=1; + ind_char = characters.next(); + count -= 1; } } diff --git a/src/trivial_demonstrations/compare_two_strings.rs b/src/trivial_demonstrations/compare_two_strings.rs index 04c16bd..a9782d2 100644 --- a/src/trivial_demonstrations/compare_two_strings.rs +++ b/src/trivial_demonstrations/compare_two_strings.rs @@ -1,29 +1,26 @@ use std::io; -fn read()->u32{ - let mut num=String::new(); +fn read() -> u32 { + let mut num = String::new(); - io::stdin() - .read_line(&mut num) - .expect("error"); - num.trim().parse::() - .unwrap() + io::stdin().read_line(&mut num).expect("error"); + num.trim().parse::().unwrap() } fn main() { println!("User 1 enter the Number"); - let a=read(); + let a = read(); println!("User 2 enter the Number"); - let b=read(); + let b = read(); println!("User 3 enter the Number"); - let c=read(); + let c = read(); - if a==b { + if a == b { println!("User 2 gussed it Right"); } else { - if a==c { + if a == c { println!("User 3 gussed it Right"); } else { println!("Both gussed it Wrong"); diff --git a/src/trivial_demonstrations/complex_numbers.rs b/src/trivial_demonstrations/complex_numbers.rs index 00aafc7..4864f0e 100644 --- a/src/trivial_demonstrations/complex_numbers.rs +++ b/src/trivial_demonstrations/complex_numbers.rs @@ -38,16 +38,22 @@ fn main() { let img2 = img2.trim(); let img2: i32 = img2.parse().unwrap(); - println!("Your First complex number is; {}+{}i",real1,img1 ); - println!("Your Second complex number is; {}+{}i",real2,img2 ); + println!("Your First complex number is; {}+{}i", real1, img1); + println!("Your Second complex number is; {}+{}i", real2, img2); - let real3 = real1+real2; - let img3 = img1+img2; + let real3 = real1 + real2; + let img3 = img1 + img2; - println!("Sum of First & Second complex number is; [{}]+[{}i]",real3,img3 ); + println!( + "Sum of First & Second complex number is; [{}]+[{}i]", + real3, img3 + ); - let real4 = real1*real2-img1*img2; - let img4 = real1*img2+real2*img1; + let real4 = real1 * real2 - img1 * img2; + let img4 = real1 * img2 + real2 * img1; - println!("Product of First & Second complex number is; [{}]+[{}i]",real4,img4 ); + println!( + "Product of First & Second complex number is; [{}]+[{}i]", + real4, img4 + ); } diff --git a/src/trivial_demonstrations/division.rs b/src/trivial_demonstrations/division.rs index f1167f2..e7b4bef 100644 --- a/src/trivial_demonstrations/division.rs +++ b/src/trivial_demonstrations/division.rs @@ -10,18 +10,16 @@ fn read_int() -> i32 { .read_line(&mut num) .expect("Error reading number"); - num.trim() - .parse::() - .unwrap() + num.trim().parse::().unwrap() } fn main() { println!("enter dividend:"); - let dividend = read_int(); // reading an integer for dividend + let dividend = read_int(); // reading an integer for dividend println!("divisor:"); - let divisor = read_int(); //reading an integer for divisor - let quotient = dividend/divisor; //calculating quotient - let remainder = dividend%divisor; //calculating remainder - println!("Quotient = {}",quotient); - println!("Remainder = {}",remainder); + let divisor = read_int(); //reading an integer for divisor + let quotient = dividend / divisor; //calculating quotient + let remainder = dividend % divisor; //calculating remainder + println!("Quotient = {}", quotient); + println!("Remainder = {}", remainder); } diff --git a/src/trivial_demonstrations/even_odd.rs b/src/trivial_demonstrations/even_odd.rs index 7e7aa78..a94b91c 100644 --- a/src/trivial_demonstrations/even_odd.rs +++ b/src/trivial_demonstrations/even_odd.rs @@ -1,4 +1,3 @@ - /* Checks whether a predefined number is odd or even. */ @@ -6,8 +5,8 @@ fn main() { let x = 5; let z = x % 2; if z == 1 { - println!("The number {} is odd",x); + println!("The number {} is odd", x); } else { - println!("The number {} is even",x); + println!("The number {} is even", x); } } diff --git a/src/trivial_demonstrations/guessing_game.rs b/src/trivial_demonstrations/guessing_game.rs index 486e5bf..c4780db 100644 --- a/src/trivial_demonstrations/guessing_game.rs +++ b/src/trivial_demonstrations/guessing_game.rs @@ -4,24 +4,23 @@ use std::io; * Matches it to a predefined number. */ fn main() { - // TODO: Randomize num - let mut num=10; + let mut num = 10; println!("Enter a number"); - while(num>0){ - let mut line=String::new(); - let input=stdin().read_line(&mut line); - let guess: Option= input.ok().map_or(None,|_| line.trim().parse().ok()); - match guess{ - None=> println!("Please Enter a Number"), + while (num > 0) { + let mut line = String::new(); + let input = stdin().read_line(&mut line); + let guess: Option = input.ok().map_or(None, |_| line.trim().parse().ok()); + match guess { + None => println!("Please Enter a Number"), Some(x) if x == num => { println!("You Guessed it Right"); break; } Some(x) if x < num => println!("TOO LOW"), - Some(x) if x > num => println!("TOO High"), - Some(_) =>println!("Error") + Some(x) if x > num => println!("TOO High"), + Some(_) => println!("Error"), } } } diff --git a/src/trivial_demonstrations/leap_year.rs b/src/trivial_demonstrations/leap_year.rs index 9737c63..9a0b401 100644 --- a/src/trivial_demonstrations/leap_year.rs +++ b/src/trivial_demonstrations/leap_year.rs @@ -2,20 +2,19 @@ use std::io; //This program will tell you whether the given year is leap year or not -fn read_int()->u32 { // function to read values - let mut num=String::new(); - io::stdin() - .read_line(&mut num) - .expect("error"); - num.trim().parse::() - .unwrap() +fn read_int() -> u32 { + // function to read values + let mut num = String::new(); + io::stdin().read_line(&mut num).expect("error"); + num.trim().parse::().unwrap() } fn main() { - let mut year=read_int(); // taking input year from user - if year%4==0 && year%100!=0 || year%400 == 0{ // condition for a leap year - println!("{} is leap year",year); - } else{ - println!("{} is not leap year",year); + let mut year = read_int(); // taking input year from user + if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 { + // condition for a leap year + println!("{} is leap year", year); + } else { + println!("{} is not leap year", year); } } diff --git a/src/trivial_demonstrations/multiplication_table.rs b/src/trivial_demonstrations/multiplication_table.rs index 2fdd431..ec19be7 100644 --- a/src/trivial_demonstrations/multiplication_table.rs +++ b/src/trivial_demonstrations/multiplication_table.rs @@ -11,15 +11,13 @@ fn read_int() -> u32 { .read_line(&mut num) .expect("Failed to read from STDIN."); - num.trim() - .parse() - .unwrap() + num.trim().parse().unwrap() } fn main() { println!("Enter the number to print multiplication table for:"); let n = read_int(); // 1..11 means 1 until 11 (excluding 11) for i in 1..11 { - println!("{}*{}={}",n,i,n*i); + println!("{}*{}={}", n, i, n * i); } } diff --git a/src/trivial_demonstrations/sum_of_a_series.rs b/src/trivial_demonstrations/sum_of_a_series.rs index 7ccc94d..33ae3bb 100644 --- a/src/trivial_demonstrations/sum_of_a_series.rs +++ b/src/trivial_demonstrations/sum_of_a_series.rs @@ -13,22 +13,20 @@ fn main() { .read_line(&mut num) .expect("Error reading number."); - let x = num.trim() - .parse::() - .unwrap(); + let x = num.trim().parse::().unwrap(); - let mut sum : i64 = 0; + let mut sum: i64 = 0; for i in 0..6 { - sum += i64::pow(-1,i)*x.pow(2*i)*fact(2*i as i64); + sum += i64::pow(-1, i) * x.pow(2 * i) * fact(2 * i as i64); } - println!("Sum of series : {}",sum); + println!("Sum of series : {}", sum); } -fn fact(n:i64) -> i64 { - if n<=1 { +fn fact(n: i64) -> i64 { + if n <= 1 { 1 } else { - n*fact(n-1) + n * fact(n - 1) } } diff --git a/src/trivial_demonstrations/sum_of_numbers.rs b/src/trivial_demonstrations/sum_of_numbers.rs index 83155e6..71afaa9 100644 --- a/src/trivial_demonstrations/sum_of_numbers.rs +++ b/src/trivial_demonstrations/sum_of_numbers.rs @@ -4,17 +4,14 @@ use std::io; * Prints the sum of first N natural numbers. */ -fn read_int()->u32{ - let mut num=String::new(); +fn read_int() -> u32 { + let mut num = String::new(); - io::stdin() - .read_line(&mut num) - .expect("error"); + io::stdin().read_line(&mut num).expect("error"); - num.trim().parse::() - .unwrap() + num.trim().parse::().unwrap() } -fn main(){ - let x=read_int(); - println!("{}",x*(x+1)/2); +fn main() { + let x = read_int(); + println!("{}", x * (x + 1) / 2); } diff --git a/src/trivial_demonstrations/sum_of_squares.rs b/src/trivial_demonstrations/sum_of_squares.rs index 35df722..414b708 100644 --- a/src/trivial_demonstrations/sum_of_squares.rs +++ b/src/trivial_demonstrations/sum_of_squares.rs @@ -1,8 +1,10 @@ use std::io; fn main() { - let mut num=String::new(); - io::stdin().read_line(&mut num).expect("Error reading number"); + let mut num = String::new(); + io::stdin() + .read_line(&mut num) + .expect("Error reading number"); let n = num.trim().parse::().unwrap(); - let f:i32=(n*(n+1)*(2*n+1))/6; - println!("sum of squares of n natural numbers ={}",f); + let f: i32 = (n * (n + 1) * (2 * n + 1)) / 6; + println!("sum of squares of n natural numbers ={}", f); } diff --git a/src/trivial_demonstrations/sum_of_two_numbers.rs b/src/trivial_demonstrations/sum_of_two_numbers.rs index 2426122..e3fcb21 100644 --- a/src/trivial_demonstrations/sum_of_two_numbers.rs +++ b/src/trivial_demonstrations/sum_of_two_numbers.rs @@ -1,13 +1,11 @@ - /* A very basic program. * Computes the sum of two variables and prints it. */ -fn main() -{ - let x=5; - let y=6; - let z=x+y; +fn main() { + let x = 5; + let y = 6; + let z = x + y; - println!("The value of z is :{}",z); + println!("The value of z is :{}", z); } diff --git a/src/trivial_demonstrations/swap.rs b/src/trivial_demonstrations/swap.rs index ee3dcd0..d8f5a8e 100644 --- a/src/trivial_demonstrations/swap.rs +++ b/src/trivial_demonstrations/swap.rs @@ -4,24 +4,21 @@ use std::io; * two variables using a temporary variable. */ -fn read_int()->u32{ - let mut num=String::new(); +fn read_int() -> u32 { + let mut num = String::new(); - io::stdin() - .read_line(&mut num) - .expect("error"); + io::stdin().read_line(&mut num).expect("error"); - num.trim().parse::() - .unwrap() + num.trim().parse::().unwrap() } fn main() { println!("Enter two numbers: "); - let mut a=read_int(); - let mut b=read_int(); + let mut a = read_int(); + let mut b = read_int(); let mut temp; - temp=a; - a=b; - b=temp; - println!("a={},b={}",a,b); + temp = a; + a = b; + b = temp; + println!("a={},b={}", a, b); }