⬆️ ⬇️

What I miss from Rust in C

About the author. Federico Mena-Quintero is a Mexican programmer, one of the founders of the GNOME project .



Librsvg has reached a turning point: it suddenly turns out that it is easier to port some of the main parts of C to Rust than to just add accessors. In addition, more and more "meat" of the library is now written in Rust.



Now I often have to switch between two languages, and C now looks very, very primitive.

')

Elegy C



C 24 . “The C Programming Language by K&R” . Turbo Pascal, , C .



K&R — . malloc()/free(), . !



C. . , Unix 20 000 .



GIMP GTK+ . GNOME , . , 20 000 C .



. .



C



POV-Ray .



GTK+ , .



SIOD, Guile — , Scheme C.



Eye of GNOME .





Evolution . Solaris Purify; Valgrind.



gnome-vfs.



Mesa.



Nautilus-share , free().



, .



, .



, - Rust, C.





, Rust, « Rust ». Rust C++: « » (RAII), , , .





// C , , , ( , )… , .



( )



Vec<T> , T. . , T.



C … .



() —



Rust — Java- - . , Java — : Drawable, draw ().



.





() . , Iterator :



pub trait Iterator {
    type Item;
    fn next(&mut self) -> Option<Self::Item>;
}


, Iterator - Item . next() , Some(YourElementType). , None.



.



, Rust for , IntoIterator:



pub trait IntoIterator {
    /// The type of the elements being iterated over.
    type Item;

    /// Which kind of iterator are we turning this into?
    type IntoIter: Iterator<Item=Self::Item>;

    fn into_iter(self) -> Self::IntoIter;
}


Item , IntoIter — Iterator, .



, . « foo bar, , ».





C , , .





:





Cargo.toml, . c .



. cargo build.





- C :





Rust :



#[test]
fn test_that_foo_works() {
    assert!(foo() == expected_result);
}


cargo test, . . Makefile, , .



-.





Rust Markdown. . :



/// Multiples the specified number by two
///
/// ```
/// assert_eq!(multiply_by_two(5), 10);
/// ```
fn multiply_by_two(x: i32) -> i32 {
    x * 2
}


, .



23.02.2018: QuietMisdreavus , rustdoc doctests . .





Rust , C. , max (5 + 3, 4) .





C, - int short, char - — Rust . .





.



, Rust



Rust , « Rust» ( , unsafe {}) . >>  — .





gcc, switch() enum, ? .



Rust . match(). , :



impl f64 {
    pub fn sin_cos(self) -> (f64, f64);
}

let angle: f64 = 42.0;
let (sin_angle, cos_angle) = angle.sin_cos();


match() . .



let color = "green";

match color {
    "red"   => println!("it's red"),
    "green" => println!("it's green"),
    _       => println!("it's something else"),
}


?



my_func(true, false, false)


, :



pub struct Fubarize(pub bool);
pub struct Frobnify(pub bool);
pub struct Bazificate(pub bool);

fn my_func(Fubarize(fub): Fubarize, 
           Frobnify(frob): Frobnify, 
           Bazificate(baz): Bazificate) {
    if fub {
        ...;
    }

    if frob && baz {
        ...;
    }
}

...

my_func(Fubarize(true), Frobnify(false), Bazificate(true));


,



. boolean , , .



#[derive(Debug)]



(, ), #[derive(Debug)] — Rust , . , gdb .





user_data.





« », . , , .



C — . Unix, . .



Rust , . , , . , , .

Source: https://habr.com/ru/post/350186/



All Articles