Rust · 771 B

Sample Rust file

An idiomatic Rust example: a customer record type, a VIP check, a total and a formatted loop — the same program in every language we offer.

CC0 · free for any use · Verified by an independent tool · Report a problem

main.rs — first linesRust
// Sample Rust program — samplefiles.com
#[derive(Debug, Clone)]
struct Customer {
    id: u32,
    name: String,
    balance: f64,
}

impl Customer {
    fn new(id: u32, name: &str, balance: f64) -> Self {
        Self { id, name: name.to_string(), balance }
    }

    fn is_vip(&self) -> bool {
        self.balance >= 10_000.0
    }
}

fn total_balance(customers: &[Customer]) -> f64 {
    customers.iter().map(|c| c.balance).sum()
}

fn main() {
    let customers = vec![Customer::new(1, "Ada", 12_500.0), Customer::new(2, "Grace", 830.5), Customer::new(3, "Linus", 10_000.0)];
    for c in &customers {

File details

Size
771 B (771 bytes)
MIME type
text/x-rust
SHA-256
8b3dda508115f43a39c1c2a1e777f603a2ae8f203692faba9f7076a37fc8a9a5
MD5
51a6c4b7bfaf4b40c690c3ef9df7251d

Download from the command line

curl -L -o main.rs https://files.samplefiles.com/code/rs/main.rs

Or check the download in your browser with the checksum calculator.

Same sample in other formats

Python · 620 BJavaScript · 642 BTypeScript · 733 BJSX · 596 BTSX · 788 BJava · 763 BKotlin · 541 BScala · 519 BC# · 689 BGo · 631 BC · 783 BC header · 353 BC++ · 925 BSwift · 665 BDart · 639 BRuby · 461 BPHP · 749 BPerl · 524 BLua · 470 BR · 417 BJulia · 479 BHaskell · 578 BElixir · 675 BErlang · 719 BClojure · 488 BF# · 508 BZig · 762 BObjective-C · 861 BBash · 425 BPowerShell · 484 BAssembly · 610 BCSS · 645 BSCSS · 508 BVue · 611 BSvelte · 397 BDockerfile · 342 BMakefile · 346 BJupyter Notebook · 1.9 KBGroovy · 501 BFortran · 843 BPascal · 1 KBOCaml · 608 BNim · 616 BSolidity · 1.2 KBVerilog · 986 BVHDL · 920 BCommon Lisp · 751 BScheme · 747 BProlog · 574 BAda · 749 BD · 553 BElm · 985 BGleam · 697 BTcl · 414 BCoffeeScript · 475 BLess · 488 BSass · 510 BCMake · 692 BWebAssembly text (WAT) · 304 BWASM · 71 BJava class · 2.1 KBPYC · 1.2 KBSource map · 1.3 KB

Similar Rust files

FileSizeDetailsDownload
Large Rust file (about 10,000 lines)sample-10000-lines.rs250 KB9,997 linesDownload