Some code from Steve Donogan's "Object Orientation in Rust" was used for this gist.
In Rust, the main()
function will automatically be run.
fn main() {
println!("Hello, world!");
}
// Hello, world!
If there is no main()
function, the compiler will print a warning.
println!("Hello, world!");
/*
No main function was detected, so your code was compiled
but not run. If you’d like to execute your code, please
add a main function.
*/
It's fine to have other blocks of code declared before the main()
function in the same file, even if those blocks of code aren't used within main()
.
trait Show {
fn show(&self) -> String;
}
impl Show for i32 {
fn show(&self) -> String {
format!("four-byte signed {}", self)
}
}
impl Show for f64 {
fn show(&self) -> String {
format!("eight-byte float {}", self)
}
}
fn main() {
println!("Hello, world!");
}
// Hello, world!