<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="fr">
	<id>https://wiki.squi.fr/index.php?action=history&amp;feed=atom&amp;title=Rust_%3A_Structs</id>
	<title>Rust : Structs - Historique des versions</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.squi.fr/index.php?action=history&amp;feed=atom&amp;title=Rust_%3A_Structs"/>
	<link rel="alternate" type="text/html" href="https://wiki.squi.fr/index.php?title=Rust_:_Structs&amp;action=history"/>
	<updated>2026-06-12T04:14:48Z</updated>
	<subtitle>Historique des versions pour cette page sur le wiki</subtitle>
	<generator>MediaWiki 1.44.0</generator>
	<entry>
		<id>https://wiki.squi.fr/index.php?title=Rust_:_Structs&amp;diff=2350&amp;oldid=prev</id>
		<title>Justine : Page créée avec « &lt;source lang=&#039;rust&#039;&gt; //Récupérer le trait debug... #[derive(Debug)] //Mon struct rectangle avec ses attributs struct Rectangle {     w: u32,     h: u32 }  //L&#039;implémentation du rectangle, avec ses méthodes //ce sont des méthodes parce qu&#039;elles ont &amp;self en paramètre //sans ça c&#039;est des &quot;associated functions&quot; impl Rectangle {     fn area(&amp;self) -&gt; u32 {         self.w * self.h     }      fn can_hold(&amp;self, other_rect: &amp;Rectangle) -&gt; bool {         if other_... »</title>
		<link rel="alternate" type="text/html" href="https://wiki.squi.fr/index.php?title=Rust_:_Structs&amp;diff=2350&amp;oldid=prev"/>
		<updated>2022-11-03T09:12:20Z</updated>

		<summary type="html">&lt;p&gt;Page créée avec « &amp;lt;source lang=&amp;#039;rust&amp;#039;&amp;gt; //Récupérer le trait debug... #[derive(Debug)] //Mon struct rectangle avec ses attributs struct Rectangle {     w: u32,     h: u32 }  //L&amp;#039;implémentation du rectangle, avec ses méthodes //ce sont des méthodes parce qu&amp;#039;elles ont &amp;amp;self en paramètre //sans ça c&amp;#039;est des &amp;quot;associated functions&amp;quot; impl Rectangle {     fn area(&amp;amp;self) -&amp;gt; u32 {         self.w * self.h     }      fn can_hold(&amp;amp;self, other_rect: &amp;amp;Rectangle) -&amp;gt; bool {         if other_... »&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nouvelle page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;source lang=&amp;#039;rust&amp;#039;&amp;gt;&lt;br /&gt;
//Récupérer le trait debug...&lt;br /&gt;
#[derive(Debug)]&lt;br /&gt;
//Mon struct rectangle avec ses attributs&lt;br /&gt;
struct Rectangle {&lt;br /&gt;
    w: u32,&lt;br /&gt;
    h: u32&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//L&amp;#039;implémentation du rectangle, avec ses méthodes&lt;br /&gt;
//ce sont des méthodes parce qu&amp;#039;elles ont &amp;amp;self en paramètre&lt;br /&gt;
//sans ça c&amp;#039;est des &amp;quot;associated functions&amp;quot;&lt;br /&gt;
impl Rectangle {&lt;br /&gt;
    fn area(&amp;amp;self) -&amp;gt; u32 {&lt;br /&gt;
        self.w * self.h&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    fn can_hold(&amp;amp;self, other_rect: &amp;amp;Rectangle) -&amp;gt; bool {&lt;br /&gt;
        if other_rect.area() &amp;lt;= self.area() {&lt;br /&gt;
            return true;&lt;br /&gt;
        } else {&lt;br /&gt;
            return false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    fn w(&amp;amp;self) -&amp;gt; String {&lt;br /&gt;
        let ret_str = String::from(&amp;quot;You called the w method. Rust is weird.&amp;quot;);&lt;br /&gt;
        return ret_str;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//On peut avoir plusieurs blocs impl si on veut&lt;br /&gt;
impl Rectangle {&lt;br /&gt;
    //Ici, une fonction associée&lt;br /&gt;
    //C&amp;#039;est un constructeur, elle me sert à construire un carré en l&amp;#039;occurence&lt;br /&gt;
    //Self est un alias pour le type qui est marqué derrière impl, donc ici&lt;br /&gt;
    //Rectangle&lt;br /&gt;
    fn square(size: u32) -&amp;gt; Self {&lt;br /&gt;
        Self {&lt;br /&gt;
            w: size,&lt;br /&gt;
            h: size&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
//On a plusieurs types de structs...&lt;br /&gt;
//Un unit struct, vide, au même titre qu&amp;#039;un unit tuple ()&lt;br /&gt;
struct Unitaire;&lt;br /&gt;
//&lt;br /&gt;
//Le struct classique donc&lt;br /&gt;
struct PlutotNormal {&lt;br /&gt;
    x: i32,&lt;br /&gt;
    y: u32&lt;br /&gt;
}&lt;br /&gt;
//&lt;br /&gt;
//Et le tuple Struct&lt;br /&gt;
struct UnSeulTruc(String);&lt;br /&gt;
struct PlusieursTrucs(i32, i32, i32);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
fn main() {&lt;br /&gt;
    let rect1 = Rectangle {&lt;br /&gt;
        w: 500,&lt;br /&gt;
        h: 1000&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
    let rect2 = Rectangle {&lt;br /&gt;
        w: 400,&lt;br /&gt;
        h: 1000&lt;br /&gt;
    };&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    //Debug en pretty-print&lt;br /&gt;
    //{:?} signifie debug, soit afficher la variable telle qu&amp;#039;écrite dans le code&lt;br /&gt;
    //{:#?} Pareil mais en plus joli&lt;br /&gt;
    //Il existe la macro dbg! qui fait la même chose&lt;br /&gt;
    //...cependant l&amp;#039;objet doit avoir le trait debug&lt;br /&gt;
    println!(&amp;quot;Mon rectangle : {:#?}&amp;quot;, rect1);&lt;br /&gt;
&lt;br /&gt;
    //Utiliser la méthode&lt;br /&gt;
    println!(&amp;quot;Area : {}&amp;quot;, rect1.area());&lt;br /&gt;
&lt;br /&gt;
    if rect1.can_hold(&amp;amp;rect2) {&lt;br /&gt;
        println!(&amp;quot;Rect1 est plus grand que rect2&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    //Une méthode de struct peut avoir le même nom qu&amp;#039;un des attributs&lt;br /&gt;
    //Ce sont alors les parenthèses qui font la diff.&lt;br /&gt;
    println!(&amp;quot;rect1.w =&amp;gt; {}&amp;quot;, rect1.w);&lt;br /&gt;
    println!(&amp;quot;rect1.w() =&amp;gt; {}&amp;quot;, rect1.w());&lt;br /&gt;
&lt;br /&gt;
    //Utiliser mon constructeur de carré&lt;br /&gt;
    let moncarre = Rectangle::square(200);&lt;br /&gt;
    dbg!(moncarre);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Justine</name></author>
	</entry>
</feed>