mirror of
https://bitbucket.org/Mattrixwv/rusttutorials.git
synced 2025-12-06 18:43:57 -05:00
Initial commit with a few examples from the website
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#vscode
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
#Ignore all executables
|
||||||
|
*.exe
|
||||||
|
|
||||||
38
debugging.rs
Normal file
38
debugging.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Derive the `fmt::Debug` implementation for `Structure`. `Structure`
|
||||||
|
// is a structure which contains a single `i32`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Structure(i32);
|
||||||
|
|
||||||
|
// Put a `Structure` inside of the structure `Deep`. Make it printable
|
||||||
|
// also.
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Deep(Structure);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Person<'a>{
|
||||||
|
name: &'a str,
|
||||||
|
age: u8
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
//Printing with `{:?}` is similar to with `{}`.
|
||||||
|
println!("{:?} months in a year.", 12);
|
||||||
|
println!("{1:?} {0:?} is the {actor:?} name.",
|
||||||
|
"Slater",
|
||||||
|
"Christian",
|
||||||
|
actor="actor's");
|
||||||
|
|
||||||
|
//`Structure` is printable!
|
||||||
|
println!("Now {:?} will print!", Structure(3));
|
||||||
|
|
||||||
|
//The problem with `derive` is there is no control over how
|
||||||
|
//the results look. What if I want this to just show a `7`?
|
||||||
|
println!("Now {:?} will print!", Deep(Structure(7)));
|
||||||
|
|
||||||
|
let name = "Peter";
|
||||||
|
let age = 27;
|
||||||
|
let peter = Person { name, age };
|
||||||
|
|
||||||
|
//Pretty print
|
||||||
|
println!("{:#?}", peter);
|
||||||
|
}
|
||||||
51
formating.rs
Normal file
51
formating.rs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
//Tutorials/hello.rs
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 06-06-20
|
||||||
|
//Modified: 06-06-20
|
||||||
|
//This is a hello world program for rust
|
||||||
|
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
// In general, the `{}` will be automatically replaced with any
|
||||||
|
// arguments. These will be stringified.
|
||||||
|
println!("{} days", 31);
|
||||||
|
|
||||||
|
// Without a suffix, 31 becomes an i32. You can change what type 31 is
|
||||||
|
// by providing a suffix. The number 31i64 for example has the type i64.
|
||||||
|
|
||||||
|
// There are various optional patterns this works with. Positional
|
||||||
|
// arguments can be used.
|
||||||
|
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
|
||||||
|
|
||||||
|
// As can named arguments.
|
||||||
|
println!("{subject} {verb} {object}",
|
||||||
|
object="the lazy dog",
|
||||||
|
subject="the quick brown fox",
|
||||||
|
verb="jumps over");
|
||||||
|
|
||||||
|
// Special formatting can be specified after a `:`.
|
||||||
|
println!("{} of {:b} people know binary, the other half doesn't", 1, 2);
|
||||||
|
|
||||||
|
// You can right-align text with a specified width. This will output
|
||||||
|
// " 1". 5 white spaces and a "1".
|
||||||
|
println!("{number:>width$}", number=1, width=6);
|
||||||
|
|
||||||
|
// You can pad numbers with extra zeroes. This will output "000001".
|
||||||
|
println!("{number:>0width$}", number=1, width=6);
|
||||||
|
|
||||||
|
// Rust even checks to make sure the correct number of arguments are
|
||||||
|
// used.
|
||||||
|
println!("My name is {0}, {1} {0}", "Bond", "James");
|
||||||
|
|
||||||
|
// Create a structure named `Structure` which contains an `i32`.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
struct Structure(i32);
|
||||||
|
|
||||||
|
// However, custom types such as this structure require more complicated
|
||||||
|
// handling. This will not work.
|
||||||
|
//println!("This struct `{}` won't print...", Structure(3));
|
||||||
|
// FIXME ^ Comment out this line.
|
||||||
|
|
||||||
|
//This prints out PI to 3 digits past the decimal
|
||||||
|
println!("PI is roughly {PI:.3}", PI=3.14159);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user