Pure Programmer
Blue Matrix


Cluster Map

Functions

L1

This page is under construction. Please come back later.

Functions1.rs
fn printHello() -> () {
	println!("Hello, world!");
}

fn main() {
	printHello();
}

Output
$ rustc Functions1.rs warning: function `printHello` should have a snake case name --> Functions1.rs:1:4 | 1 | fn printHello() -> () { | ^^^^^^^^^^ help: convert the identifier to snake case: `print_hello` | = note: `#[warn(non_snake_case)]` on by default warning: 1 warning emitted $ ./Functions1 Hello, world!
Functions2.rs
fn printHello(name:&str) -> () {
	println!("Hello, {}!", name);
}

fn main() {
	printHello("Fred");
	printHello("Wilma");
	printHello("Barney");
	printHello("Betty");
}

Output
$ rustc Functions2.rs warning: function `printHello` should have a snake case name --> Functions2.rs:1:4 | 1 | fn printHello(name:&str) -> () { | ^^^^^^^^^^ help: convert the identifier to snake case: `print_hello` | = note: `#[warn(non_snake_case)]` on by default warning: 1 warning emitted $ ./Functions2 Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty!
Functions3.rs
fn squared(j:isize) -> isize {
	return j * j;
}

fn main() {
	{
		let mut i:isize = 1;
		while i <= 10 {
			println!("{}", format!("{0:d}^2 = {1:d}", i, squared(i)));
			i += 1;
		}
	}
}

Output
$ rustc Functions3.rs error: unknown format trait `d` --> Functions3.rs:9:31 | 9 | println!("{}", format!("{0:d}^2 = {1:d}", i, squared(i))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> Functions3.rs:9:41 | 9 | println!("{}", format!("{0:d}^2 = {1:d}", i, squared(i))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: aborting due to 2 previous errors
Functions4.rs
fn exponentiation(base:f64, powerArg:isize) -> f64 {
	let mut power:isize = powerArg;
	let mut result:f64 = 1;
	while power > 0 {
		result *= base;
		power -= 1;
	}
	return result;
}

fn main() {
	{
		let mut b:f64 = 1.1f64;
		while b < 2.05f64 {
			{
				let mut p:isize = 2;
				while p <= 10 {
					println!("{}", format!("{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p)));
					p += 1;
				}
			}
			b += 0.1f64;
		}
	}
}

Output
$ rustc Functions4.rs error: unknown format trait `f` --> Functions4.rs:18:33 | 18 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `d` --> Functions4.rs:18:41 | 18 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> Functions4.rs:18:49 | 18 | println!("{}", format!("{0:f} ^ {1:d} = {2:f}", b, p, exponentiation(b, p))); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0308]: mismatched types --> Functions4.rs:3:23 | 3 | let mut result:f64 = 1; | --- ^ | | | | | expected `f64`, found integer | | help: use a float literal: `1.0` | expected due to this error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0308`.
Functions5.rs
fn factorial(x:isize) -> i64 {
	if x <= 1 {
		return 1;
	}
	return x * factorial(x - 1);
}

fn main() {
	{
		let mut x:isize = 1;
		while x < 10 {
			println!("{}", format!("{0:d}! = {1:d}", x, factorial(x)));
			x += 1.0;
		}
	}
}

Output
File not found!: /kunden/homepages/39/d957328751/htdocs/pureprogrammer/rs/examples/output/Functions5.out
Lists6.rs
mod utils;

fn reverseList(x:Vec<String>) -> Vec<String> {
	let mut y:Vec<String> = Vec::new();
	{
		let mut i:isize = x.len() - 1;
		while i >= 0 {
			y.push(x[i]);
			i += -1;
		}
	}
	return y;
}

fn main() {
	let mut names:Vec<&str> = vec!["Fred", "Wilma", "Barney", "Betty"];

	for name in names {
		println!("Hello, {}!", name);
	}

	names = reverseList(names);
	for name in names {
		println!("Hello, {}!", name);
	}

	println!("{}", utils::list_to_string(names));
}

Output
$ rustc Lists6.rs error[E0308]: mismatched types --> Lists6.rs:6:21 | 6 | let mut i:isize = x.len() - 1; | ----- ^^^^^^^^^^^ expected `isize`, found `usize` | | | expected due to this | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 6 | let mut i:isize = (x.len() - 1).try_into().unwrap(); | + +++++++++++++++++++++ error[E0277]: the type `[String]` cannot be indexed by `isize` --> Lists6.rs:8:13 | 8 | y.push(x[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[String]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<String>` to implement `Index<isize>` error[E0308]: mismatched types --> Lists6.rs:22:22 | 22 | names = reverseList(names); | ----------- ^^^^^ expected `Vec<String>`, found `Vec<&str>` | | | arguments to this function are incorrect | = note: expected struct `Vec<String>` found struct `Vec<&str>` note: function defined here --> Lists6.rs:3:4 | 3 | fn reverseList(x:Vec<String>) -> Vec<String> { | ^^^^^^^^^^^ ------------- error[E0308]: mismatched types --> Lists6.rs:22:10 | 16 | let mut names:Vec<&str> = vec!["Fred", "Wilma", "Barney", "Betty"]; | --------- expected due to this type ... 22 | names = reverseList(names); | ^^^^^^^^^^^^^^^^^^ expected `Vec<&str>`, found `Vec<String>` | = note: expected struct `Vec<&str>` found struct `Vec<String>` error: aborting due to 4 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`.
Maps3.rs
mod utils;
use std::collections::HashMap;

fn convertKMtoMiles(x:HashMap<String, isize>) -> HashMap<String, isize> {
	let mut y:HashMap<String, isize> = HashMap::new();
	for planet in Utils::keys(x) {
		y[planet] = ((x.at(planet)) as f64 * 0.621371f64 + 0.5f64) as isize;
	}
	return y;
}

fn main() {
	let mut planetDiametersInKM:HashMap<String, isize> = [
		{"Mercury", 4879},
		{"Venus", 12103},
		{"Earth", 12756},
		{"Mars", 6794},
		{"Jupiter", 142985},
		{"Saturn", 120534},
		{"Uranus", 51115},
		{"Neptune", 49534},
		{"Pluto", 2374},
		{"Ceres", 946},
		{"Eris", 2326},
		{"Makemake", 1430}
	];

	let mut planetDiametersInMiles:HashMap<String, isize> = convertKMtoMiles(planetDiametersInKM);
	for planet in Utils::keys(planetDiametersInMiles) {
		println!("{} has a diameter of {} miles", planet, planetDiametersInMiles.at(planet));
	}
}

Output
$ rustc Maps3.rs error: this is a block expression, not an array --> Maps3.rs:14:3 | 14 | {"Mercury", 4879}, | ^^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 14 | ["Mercury", 4879], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:15:3 | 15 | {"Venus", 12103}, | ^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 15 | ["Venus", 12103], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:16:3 | 16 | {"Earth", 12756}, | ^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 16 | ["Earth", 12756], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:17:3 | 17 | {"Mars", 6794}, | ^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 17 | ["Mars", 6794], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:18:3 | 18 | {"Jupiter", 142985}, | ^^^^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 18 | ["Jupiter", 142985], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:19:3 | 19 | {"Saturn", 120534}, | ^^^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 19 | ["Saturn", 120534], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:20:3 | 20 | {"Uranus", 51115}, | ^^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 20 | ["Uranus", 51115], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:21:3 | 21 | {"Neptune", 49534}, | ^^^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 21 | ["Neptune", 49534], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:22:3 | 22 | {"Pluto", 2374}, | ^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 22 | ["Pluto", 2374], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:23:3 | 23 | {"Ceres", 946}, | ^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 23 | ["Ceres", 946], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:24:3 | 24 | {"Eris", 2326}, | ^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 24 | ["Eris", 2326], | ~ ~ error: this is a block expression, not an array --> Maps3.rs:25:3 | 25 | {"Makemake", 1430} | ^^^^^^^^^^^^^^^^^^ | help: to make an array, use square brackets instead of curly braces | 25 | ["Makemake", 1430] | ~ ~ error[E0433]: failed to resolve: use of undeclared type `Utils` --> Maps3.rs:6:16 | 6 | for planet in Utils::keys(x) { | ^^^^^ use of undeclared type `Utils` error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> Maps3.rs:7:19 | 7 | y[planet] = ((x.at(planet)) as f64 * 0.621371f64 + 0.5f64) as isize; | ^^ method not found in `HashMap<String, isize>` error[E0433]: failed to resolve: use of undeclared type `Utils` --> Maps3.rs:29:16 | 29 | for planet in Utils::keys(planetDiametersInMiles) { | ^^^^^ use of undeclared type `Utils` error[E0599]: no method named `at` found for struct `HashMap` in the current scope --> Maps3.rs:30:76 | 30 | println!("{} has a diameter of {} miles", planet, planetDiametersInMiles.at(planet)); | ^^ method not found in `HashMap<String, isize>` error: aborting due to 16 previous errors Some errors have detailed explanations: E0433, E0599. For more information about an error, try `rustc --explain E0433`.
Tuples3.rs
fn swap(x:(String, isize)) -> (isize, String) {
	let mut y:(isize, String);
	y = [x.1, x.0];
	return y;
}

fn main() {
	let mut pair1 = ("Hello", 5);
	let mut pair2:(isize, String);
	pair2 = swap(pair1);
	println!("{} becomes {}", Utils.tupleToString(pair1), Utils.tupleToString(pair2));
}

Output
$ rustc Tuples3.rs error[E0425]: cannot find value `Utils` in this scope --> Tuples3.rs:11:28 | 11 | println!("{} becomes {}", Utils.tupleToString(pair1), Utils.tupleToString(pair2)); | ^^^^^ not found in this scope error[E0425]: cannot find value `Utils` in this scope --> Tuples3.rs:11:56 | 11 | println!("{} becomes {}", Utils.tupleToString(pair1), Utils.tupleToString(pair2)); | ^^^^^ not found in this scope error[E0308]: mismatched types --> Tuples3.rs:3:12 | 3 | y = [x.1, x.0]; | ^^^ expected `isize`, found `String` error[E0308]: mismatched types --> Tuples3.rs:10:15 | 10 | pair2 = swap(pair1); | ---- ^^^^^ expected `(String, isize)`, found `(&str, {integer})` | | | arguments to this function are incorrect | = note: expected tuple `(String, isize)` found tuple `(&str, {integer})` note: function defined here --> Tuples3.rs:1:4 | 1 | fn swap(x:(String, isize)) -> (isize, String) { | ^^^^ ----------------- error: aborting due to 4 previous errors Some errors have detailed explanations: E0308, E0425. For more information about an error, try `rustc --explain E0308`.

Questions

Projects

More ★'s indicate higher difficulty level.

References