Menu

Learning Rust
📰
229

Learning Rust

Hacker News·Learning Rust·about 1 month ago
#QdM6A63R
Reading 0:00
15s threshold

Rust Programming Language Tutorials for Everyone! Data and behavior struct Person { name : String , company_name : String , } impl Person { fn new ( name : String , company_name : String ) -> Self { Self { name , company_name } } fn intro ( & self ) -> String { format! ( "I'm {} from {} " , self . name , self . company_name ) } } trait Greet { const PREFIX : & 'static str = "Hello" ; fn greet ( & self ) -> String { format! ( " {} !" , String :: from ( Self :: PREFIX )) } } impl Greet for Person {} fn main () { let steve = Person :: new ( "Steve" . to_string (), "Apple" . to_string ()); println! ( " {} " , steve . greet ()); // Hello! println! ( " {} " , steve . intro ()); // I'm Steve from Apple } Functional fn main () { let marks = [ "10" , "20" , "30" , "0.4" , "0.5" , "invalid" ]; let sum : i32 = marks // Iterate through marks . iter () // Parse string to float, ignore if invalid . filter_map ( |& s | s . parse :: < f64 > (). ok ()) // Multiply decimals under 1.0 by 100 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More