mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-09 20:03:24 +03:00
Inital commit of learning Rust
This commit is contained in:
parent
9ec35d899c
commit
ac54c9ded2
|
@ -1,8 +1,11 @@
|
||||||
// functions1.rs
|
// functions1.rs
|
||||||
// Make me compile! Execute `rustlings hint functions1` for hints :)
|
// Make me compile! Execute `rustlings hint functions1` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
// This needed the call_me fucntion to be defined.
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
fn call_me() {
|
||||||
|
println!("Hello World");
|
||||||
|
}
|
||||||
call_me();
|
call_me();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
// functions2.rs
|
// functions2.rs
|
||||||
// Make me compile! Execute `rustlings hint functions2` for hints :)
|
// Make me compile! Execute `rustlings hint functions2` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
call_me(3);
|
call_me(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call_me(num:) {
|
// Below I added the i32, beacuse the function expected a type to set/specified.
|
||||||
|
fn call_me(num: i32) {
|
||||||
for i in 0..num {
|
for i in 0..num {
|
||||||
println!("Ring! Call number {}", i + 1);
|
println!("Ring! Call number {}", i + 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
// functions3.rs
|
// functions3.rs
|
||||||
// Make me compile! Execute `rustlings hint functions3` for hints :)
|
// Make me compile! Execute `rustlings hint functions3` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
// Need to pass in an unsigned 32 bit num to get this to work.
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
call_me();
|
call_me(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call_me(num: u32) {
|
fn call_me(num: u32) {
|
||||||
|
|
|
@ -4,14 +4,14 @@
|
||||||
// This store is having a sale where if the price is an even number, you get
|
// This store is having a sale where if the price is an even number, you get
|
||||||
// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
|
// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let original_price = 51;
|
let original_price = 51;
|
||||||
println!("Your sale price is {}", sale_price(original_price));
|
println!("Your sale price is {}", sale_price(original_price));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sale_price(price: i32) -> {
|
// Below I needed to specify a type for the return item
|
||||||
|
// This happened to be a i32
|
||||||
|
fn sale_price(price: i32) -> i32 {
|
||||||
if is_even(price) {
|
if is_even(price) {
|
||||||
price - 10
|
price - 10
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
// functions5.rs
|
// functions5.rs
|
||||||
// Make me compile! Execute `rustlings hint functions5` for hints :)
|
// Make me compile! Execute `rustlings hint functions5` for hints :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let answer = square(3);
|
let answer = square(3);
|
||||||
println!("The answer is {}", answer);
|
println!("The answer is {}", answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This funtion implicitly returns, there is no need for the ; after num * num
|
||||||
fn square(num: i32) -> i32 {
|
fn square(num: i32) -> i32 {
|
||||||
num * num;
|
num * num
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
// if1.rs
|
// if1.rs
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
pub fn bigger(a: i32, b: i32) -> i32 {
|
pub fn bigger(a: i32, b: i32) -> i32 {
|
||||||
// Complete this function to return the bigger number!
|
// Complete this function to return the bigger number!
|
||||||
// Do not use:
|
// Do not use:
|
||||||
// - another function call
|
// - another function call
|
||||||
// - additional variables
|
// - additional variables
|
||||||
// Execute `rustlings hint if1` for hints
|
// Execute `rustlings hint if1` for hints
|
||||||
|
if a > b {
|
||||||
|
a
|
||||||
|
} else {
|
||||||
|
b
|
||||||
|
}
|
||||||
|
// No need for a return a; these fucnitons implictily return!
|
||||||
}
|
}
|
||||||
|
|
||||||
// Don't mind this for now :)
|
// Don't mind this for now :)
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
// ready for the next exercise, remove the `I AM NOT DONE` comment below.
|
// ready for the next exercise, remove the `I AM NOT DONE` comment below.
|
||||||
// Execute the command `rustlings hint intro1` for a hint.
|
// Execute the command `rustlings hint intro1` for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello and");
|
println!("Hello and");
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
// Make the code print a greeting to the world.
|
// Make the code print a greeting to the world.
|
||||||
// Execute `rustlings hint intro2` for a hint.
|
// Execute `rustlings hint intro2` for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello {}!");
|
let n = "World";
|
||||||
|
println!("Hello {}!", n);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
// Make me compile!
|
// Make me compile!
|
||||||
// Execute the command `rustlings hint variables1` if you want a hint :)
|
// Execute the command `rustlings hint variables1` if you want a hint :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
x = 5;
|
let x = 5;
|
||||||
|
// When using string formating the value needs to be assigned before it can
|
||||||
|
// be used, standard stuff.
|
||||||
println!("x has the value {}", x);
|
println!("x has the value {}", x);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
// variables2.rs
|
// variables2.rs
|
||||||
// Make me compile! Execute the command `rustlings hint variables2` if you want a hint :)
|
// Make me compile! Execute the command `rustlings hint variables2` if you want a hint :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x;
|
let x = 1; // Orginally let x; Variables need defining from what I can tell.
|
||||||
if x == 10 {
|
if x == 10 {
|
||||||
println!("Ten!");
|
println!("Ten!");
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
// variables3.rs
|
// variables3.rs
|
||||||
// Make me compile! Execute the command `rustlings hint variables3` if you want a hint :)
|
// Make me compile! Execute the command `rustlings hint variables3` if you want a hint :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = 3;
|
let mut x = 5;
|
||||||
println!("Number {}", x);
|
println!("Number {}", x);
|
||||||
x = 5; // don't change this line
|
x = 5; // don't change this line
|
||||||
println!("Number {}", x);
|
println!("Number {}", x);
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
// variables4.rs
|
// variables4.rs
|
||||||
// Make me compile! Execute the command `rustlings hint variables4` if you want a hint :)
|
// Make me compile! Execute the command `rustlings hint variables4` if you want a hint :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let x: i32;
|
let x: i32;
|
||||||
|
x = 6;
|
||||||
println!("Number {}", x);
|
println!("Number {}", x);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
// variables5.rs
|
// variables5.rs
|
||||||
// Make me compile! Execute the command `rustlings hint variables5` if you want a hint :)
|
// Make me compile! Execute the command `rustlings hint variables5` if you want a hint :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let number = "T-H-R-E-E"; // don't change this line
|
let number = "T-H-R-E-E"; // don't change this line
|
||||||
println!("Spell a Number : {}", number);
|
println!("Spell a Number : {}", number);
|
||||||
number = 3;
|
let number = 3; // if we want to reuse a varibale name we need to use
|
||||||
|
// Shadowing, this is different from making a var mutable
|
||||||
println!("Number plus two is : {}", number + 2);
|
println!("Number plus two is : {}", number + 2);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
// variables6.rs
|
// variables6.rs
|
||||||
// Make me compile! Execute the command `rustlings hint variables6` if you want a hint :)
|
// Make me compile! Execute the command `rustlings hint variables6` if you want a hint :)
|
||||||
|
|
||||||
// I AM NOT DONE
|
const NUMBER: i32 = 3; // Consts need to be assigned a type
|
||||||
|
|
||||||
const NUMBER = 3;
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Number {}", NUMBER);
|
println!("Number {}", NUMBER);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue